類的變量初始化[重復]

[英]variable initialization of a class [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

I have a doubt regarding the initialization of variables of a class. When usually declare a class with the variables and I initialize (even if they are final) I do it via the constructor.

我對類的變量的初始化有疑問。當通常用變量聲明一個類並且我初始化時(即使它們是最終的)我通過構造函數來做。

For example:

class Example {
    private int a, b;
   
    public Example () {
        a = 5;
        b = 10;
    }

    // Methods
}

But you can also initiate immediately after the statement.

但您也可以在聲明后立即發起。

For example:

class Example {
    private int a = 5, b = 10;
   
    // Methods
}

What is the best way to initialize variables? What is the difference?

初始化變量的最佳方法是什么?有什么不同?

2 个解决方案

#1


-1  

The variables initialized the first way will get their values when the init<> method is run. The variable initialized in the second way will get their value when the constructor is executed. For simple use cases like this there is no effective difference, but if you have multiple constructors or "interesting" things happening with the object lifecycle, it can matter. There is no always-better way.

初始化第一種方式的變量將在init <>方法運行時獲取它們的值。以第二種方式初始化的變量將在執行構造函數時獲得它們的值。對於這樣的簡單用例,沒有任何有效的區別,但如果你有多個構造函數或對象生命周期中發生的“有趣”事情,那就很重要了。沒有永遠更好的方法。

(One example of how you'd get different behavior is if a superclass' constructor - which must run before the subclass - used reflection to act based on the instance field variables somehow. This isn't a very common thing to need to happen, but it's an example of what I meant by "interesting"...)

(你如何獲得不同行為的一個例子是,如果一個超類'構造函數 - 必須在子類之前運行 - 使用反射基於實例字段變量以某種方式運行。這不是一件非常常見的事情,需要發生,但它是我所說的“有趣”的一個例子...)

#2


0  

class Foo {
   String username = "user";
   String password = null;
}

is functionally equivalent to

在功能上等同於

class Foo {
    String username, password;
    Foo() { 
        username = "user";
        passowrd = null; 
    }
}

Consider however, a situation that at some later point you need to add another constructor:

但是請考慮一種情況,稍后您需要添加另一個構造函數:

public Foo(String password) {
    this.password = password;
}

Now, somebody calling new Foo("mypasswd") is risking to have username left uninitialized. If you initialize the member variables as they are declared, they will always be initialized, regardless of how the instance of the class was created (except for deserialization, that's a separate story!). If you initialize them in the default constructor, then all other constructors need to either call it explicitly or take care of initializing the variables themselves.

現在,有人調用新的Foo(“mypasswd”)可能會讓用戶名未被初始化。如果在聲明成員變量時初始化它們,那么無論如何創建類的實例,它們都將被初始化(除了反序列化,這是一個單獨的故事!)。如果在默認構造函數中初始化它們,那么所有其他構造函數需要顯式調用它或者負責初始化變量本身。

It is advisable to always give all variables some kind of an initial value when you declare them, to reduce the possibility of surprises and unpredictable behavior.

建議在聲明它們時始終為所有變量賦予某種初始值,以減少意外和不可預測行為的可能性。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/01/13/72f12fc898e5f273c04898c0eada094a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com