What is the difference between:
pu开发者_运维百科blic class A
{
private int x = 1;
A() {}
}
and
public class A
{
private int x;
A() { x = 1; }
}
, if any?
If you are asking from a practical point of view, the difference is that with the second form of initialization you will have to repeat it for every constructor that you write, were you to write many overloaded constructors.
- In second case you repeating the of initializing x=0 because as it is instance variable so it will be initialized to 0 by default.
- This can be difference if multiple constructors will be there.else i dont think any other difference.
1/ The written assignments happen in different times during initialization - the constructor is the last thing executed during instance initialization.
2/ There is implicit initialization to zero for the x variable provided by compiler. So both assignment are redundant.
From JLS 12.5:
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden.
Further down it states:
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
In essence, the JVM creates memory for variable x
(as well as all instance variables for superclasses) and initialize each instances with a default value (0
for x
). Before the new instance of class A
is returned, it will now execute the constructor body.
Effectively nothing. Variables at class-scope will have a default value initialised if you do not initialise it yourself. For the int
type, this will be 0. There's a table of default initialisation values for primitives here.
It is important to note that this does not hold true for local primitives, and that you should always initialise these to a value before use.
the difference is that in the second you can have a constructor
public class A
{
private int x;
A(String something) { }
}
and if you add the second constructor and you forget to call the this() then you dont initialise your x leaving it at its default. So i suggest you use the first one as it is inhertly less bug prone.
In the first case,
You do not need to initialize the variable to 0, as by default all the member variables are initialized to 0.
You do not need to write a no arg constructor as it will also be given by default.
In the second case, you again dont need that constructor which is setting the variable to 0. It can be needed if you want to give some value other than 0, say x=20;
In this case, nothing.
If x was static, then it would not get initialised, until "new A();" was coded.
Seeing as x is not static, the processing is effectively the same, however there are nuances in the JLS that you should be aware of, especially if A extends another class, for example.
If you did something with x in the constructor BEFORE it was initialised (as in example 2), e.g. int b = x; do not expect b to be 1. Off the top of my head, you'll either get an error/ warning on the compiler, or b would equal zero.
精彩评论