Reputation: 8903
I am having difficulty in understanding how an instance variable gets initialized when we declare it. Consider the following snippet:
class Rat {
private int val = 20;
}
class Cat {
private Rat rat = new Rat();
}
I am not able to understand how we initialize the instance variables at the time of their declaration within class; aren't these variables supposed to get their "values" or "references" when we actually create an object, for example in main we do:
main() {
Cat cat = new Cat();
}
So that each object have their "own" set of instance variables; and for example to initialize Rat instance variable of Cat, we should do:
cat.Rat = new Rat();
Can anyone help me understand this.
Upvotes: 0
Views: 1895
Reputation: 51
class Rat {
private int val = 20;
}
Until you create instance of Rat, the variable val doesn't hold value '20'. So, In order for JVM to assign value 20 to val, you need to create Rat instance with 'new' operator.
Similarly,
class Cat {
private Rat rat = new Rat();
}
Unless you create instance of Cat using 'new' operator, variable rat will not get initialized with 'new Rat()' instance.
Similarly for Cat instance and initialization
Finally, static variable's get initialized at the time of class loading and available till the class loader is live And instance variables are initialized at the time of Class's instantiation and based on its scope i.e global variable or Local Variable.
Please read about the scope to know more about it.
Upvotes: 0
Reputation: 816
From the below code snippet,
main() {
Cat cat = new Cat();
}
It is evident that, we are trying to create an Object 'Cat'. So, when we call this, the Cat Class would be loaded(if it is not already loaded). While the Class is being loaded, it will initialize the instance variables.
In the below scenario,
class Cat {
private Rat rat = new Rat();
}
As the Cat class has an instance variable which is dependent on another class called Rat, Rat class would be loaded and during the process, instance variable in Rat would be initialized.
class Rat {
private int val = 20;
}
So, the above code snippet would be initialized first, then this assignment would be done private Rat rat = new Rat();
and finally the Cat object( Cat cat = new Cat();
) would be created and assigned it to the cat
reference variable.
Upvotes: 1
Reputation: 1695
As per definition in https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.5 of the JVM the fields are initialized in step 4 of the creation of a new instance of the class. So before the body of your constructor is executed.
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation (§8.8.7.1) 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.
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.
Upvotes: 1
Reputation: 393781
I am not able to understand how we initialize the instance variables at the time of their declaration within class
The instance variables are initialized when an instance is created. Each time you create an instance of Cat
, private Rat rat = new Rat();
is executed. Each time you create an instance of Rat
, private int val = 20;
is executed (right after the execution of the super-class constructor and before the execution of the current class's constructor body).
When
Cat cat = new Cat();
is executed, right before the Cat
constructor's body is executed, the instance variables of Cat
are initialized, so
private Rat rat = new Rat();
is executed. Now, right before the Rat
constructor's body is executed, the instance variables of Rat
are initialized, so
private int val = 20;
is executed.
Upvotes: 4