Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between instantiating in the field and instantiating in the constructor?

Tags:

java

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
}
like image 526
Jeune Avatar asked Oct 18 '09 16:10

Jeune


People also ask

What is a constructor instantiation?

When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.

What is the difference between initializing and instantiating?

Initialization: Assigning a value to a variable is called initialization. For example, cost = 100. It sets the initial value of the variable cost to 100. Instantiation: Creating an object by using the new keyword is called instantiation.

What is the difference between initializing and instantiating Java?

1) Declaration: The code set in bold are all variable declarations that associate a variable name with an object type. 2) Instantiation: The new keyword is a Java operator that creates the object. 3) Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

What does instantiating a class means?

When a copy of a class is created that inherits all the class properties and functions, it is called instantiating a class. To instantiate a class in Python, the class like it is called a function, passing the arguments defined by the __init__ method. The newly created object is the return value.


1 Answers

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:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
  2. 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.
  3. 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.)
  5. 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.

like image 185
Pascal Thivent Avatar answered Sep 27 '22 17:09

Pascal Thivent