Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do instance variables get initialized and values assigned?

When does the instance variable get initialized? Is it after the constructor block is done or before it?

Consider this example:

public abstract class Parent {

 public Parent(){
   System.out.println("Parent Constructor");
   init();
 }

 public void init(){
   System.out.println("parent Init()");
 }
}

public class Child extends Parent {

private Integer attribute1;
private Integer attribute2 = null;

public Child(){
    super();
    System.out.println("Child Constructor");
}

public void init(){
    System.out.println("Child init()");
    super.init();
    attribute1 = new Integer(100);
    attribute2 = new Integer(200);
}

public void print(){
    System.out.println("attribute 1 : " +attribute1);
    System.out.println("attribute 2 : " +attribute2);
}
}

public class Tester {

public static void main(String[] args) {
    Parent c = new Child();
    ((Child)c).print();
    
}
}

OUTPUT:

Parent Constructor

Child init()

parent Init()

Child Constructor

attribute 1 : 100

attribute 2 : null


  1. When are the memory for the atribute 1 & 2 allocated in the heap ?

  2. Curious to know why is attribute 2 is NULL ?

  3. Are there any design flaws?

like image 828
Akh Avatar asked Sep 06 '12 21:09

Akh


People also ask

Where should the instance variables get their initial values?

Instance variables are declared at the same level as methods within a class definition. They are usually given private access to restrict visibility. They can receive initial values either when they are declared or in a constructor. Instances variable references may or may not be prefixed with the reserved word this.

When instance variables are initialized in Java?

The instance variable is initialized at the time of the class loading or when an object of the class is created. An instance variable can be declared using different access modifiers available in Java like default, private, public, and protected.

Are instance variables initialized to a default value?

Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor.

When instance variables are created?

Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable. An instance variable is not a class variable although there are similarities.


1 Answers

When the memory for the atribute 1 & 2 are allocated in the heap ?

The memory for the object as a whole is allocated when the new operator is invoked, before the java.lang.Object constructor is entered. Memory is allocated for individual Integer instances in init, but there is no point when memory is allocated for individual properties -- only whole objects.

Curious to know why is attribute 2 is NULL ?

The init method is called in the super constructor, so attribute2 is assigned new Integer(200), and then the subclass constructor is invoked which applies property initializers in the order they appear in the source code. This line

private Integer attribute2 = null;

overwrites the value assigned by init() to null.

If you add a call to

 System.out.println("attribute 2 : " +attribute2);

right after your call to super(); then this will become apparent.

Are there any design flaws?

Calling sub-class methods before the base class has finished initializing is dangerous. The sub-class might rely on its base-class's invariants to protect its own invariants, and if the base-class constructor has not completed, then its invariants may not hold.

This is also likely to confuse C++ programmers and the like who would expect a call to init from the base class to invoke the base class's version since C++ rewrites the vtable pointer as constructors are entered.

See The Java Language Specification for all the gory details.

like image 56
Mike Samuel Avatar answered Oct 05 '22 10:10

Mike Samuel