Reputation: 3538
What's the difference between doing this:
public class SomeClass {
SomeObject obj = new SomeObject();
//rest of the code
}
and this
public class SomeClass {
SomeObject obj;
public SomeClass(){
obj = new SomeObject();
}
//rest of the code
}
Upvotes: 14
Views: 4988
Reputation: 7290
One aspect not yet mentioned:
public class SomeClass {
SomeObject obj = new SomeObject();
//rest of the code
}
will initialize obj to a fixed value. But when initializing in a constructor, you can have the initialization depend on the constructor's parameters, or (with multiple constructors) use entirely different initialization expressions, e.g.
public class SomeClass {
private SomeObject obj;
public SomeClass(int length){
obj = new SomeObject(3 * length + 7);
}
//rest of the code
}
Upvotes: 0
Reputation: 39485
In the first instance, obj
will be initialized before the constructor is run. This is an important nuance when you have subclasses. The ordering of constructors and initialization blocks will be:
Upvotes: 0
Reputation: 56752
This is only a matter of style, it compiles to the same code.
Personally I tend to put all initialization of instances in constructors, because that works uniformly for all cases.
Upvotes: 1
Reputation: 570295
According to the chapter 12.5 Creation of New Class Instances of the Java Language Specification:
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
- 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.
So the difference is just the step (step 4. or step 5.) but the result is the same.
Upvotes: 16
Reputation: 57648
The only difference is in at which step the reference is initialized. The final effect is the same.
Upvotes: 2
Reputation: 46770
Time of construction. The first is done before main is entered. The other object's construction is delayed until SomeClass's ctor is called.
Upvotes: -3