1

After reading various links on stackoverflow, I have this understanding:

Is this correct:

  • Instance variables are initialized when the class is instantiated.
  • If instance variables are declared only and not initialized, they will be assigned default values by JVM before constructor execution.
  • If instance variables are declared with initialization, then these lines will be moved within every constructor of my class. And thus, execution will be done in constructor by compiler.

Thanks.

  • You can find all the steps in the Java Language Specification section 12.5. The language may be a bit technical, but it's the definitive source. What you wrote above is close, but not entirely correct. For example, instance variables are *always* first assigned their default values, no matter if they have initializers or not. For Java 8: https://docs.oracle.com/javase/specs/jls/se10/html/jls-12.html#jls-12.5 – Erwin Bolwidt May 11 '18 at 07:13
  • I understand following flow of execution from the above link: 1. Memory allocation (assuming required memory is calculated by JVM) 2. Default value allocation to corresponding instance variables by JVM 3. Constructor execution (first line) 4. Instance initializers execution (assuming constructor execution is stopped) 5. Remaining constructor execution. Still unclear when variables which are initialized at declaration are executed? – Muhammad Abu Bakr May 11 '18 at 07:35
  • Plus, now the below answer says, "I can say initialization at declaration is done by compiler before constructor execution". made me more confused – Muhammad Abu Bakr May 11 '18 at 07:39
  • That's done in step 4: Execute the instance initializers and **instance variable initializers** for this class – Erwin Bolwidt May 11 '18 at 07:40
  • 1
    Thanks, last thing: Does compiler moves instance variable initializers in the constructor behind the scenes? Or is people just say this for better understanding. And it is because: compiler stops after first line (recursive calls to super() or this()) to run instance variable initializers (not moved to constructor) and after this executes the remaining constructor code? – Muhammad Abu Bakr May 11 '18 at 07:53
  • In principle, a Java compiler is free to choose any way to do this as long as the JLS is satisfied - perhaps it could move initialization to a separate private method (I haven't fully thought about the complications of that) but in practice, javac copies the initialization to every constructor in the .class file. You can try with a simple class `class A { int i = 10; A() { } A(int b) { } }`, compile it and then do `javap -c A` to confirm that the bytecode to initialize i to 10 is in both constructors. – Erwin Bolwidt May 11 '18 at 08:32

2 Answers2

1

instance variable belongs to object of a class,So when object of a class is created instance variable get seprate memory in each object creation.

0

Here are the possible ways to initialize an instance variable, and their order:

class MyClass {
    private int var1 = 1;
    private int var2;
    {
        var2 = 2;
    }

    private int var3;

    public MyClass() {
        var3 = 3;
    }
}

You're correct about the default values (eg 0 for int, false for boolean, null for objects).

You can't really say that initializations at declaration are "moved into constructor of my class"; for instance, if an initialization in the declaration throws an exception, it causes an ExceptionInInitializerError which is something you cannot (ie shouldn't) catch.

daniu
  • 14,137
  • 4
  • 32
  • 53