1

If I have the following code:

public class Test {

   private int x = 5;
   private static int y = 5;

   public Test() {
      x = 10;
      y = 10;
   }
}

I'm wondering in both cases will it actually initially assign 5, and then update this with 10, in other words, there's no point initialising a variable inline and in a constructor as it actually has the effect of initialising a variable twice? Or in the case of x (being an instance field) does it just replace x = 5 with x = 10 and therefore only even run x = 10?

It would be nice to know the decompiled version.

M A
  • 71,713
  • 13
  • 134
  • 174
Tranquility
  • 3,061
  • 5
  • 23
  • 37

5 Answers5

2

I'm wondering in both cases will it actually initially assign 5, and then update this with 10

Yes. The constructor will:

  1. Call super().
  2. Execute any inline initializations and anonymous initialization blocks.
  3. Execute the remaining body of the constructor.

This is specified in more detail in the JLS #12.5-6.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

The compiler will not do such kinds of optimization - there is actually no point in doing such a micro-optimization (you can make sure using javap).

Furthermore the language specification dictates that such values be assigned in some order. And these steps should logically be different steps that are not combined.

M A
  • 71,713
  • 13
  • 134
  • 174
-1

Simple flow is:
1. Parent class initialization
2. Static fields and static sections initialization in the order they occur in the source file
3. Fields initialization and instance sections initialization in the order they occur in the source file
4. Constructor execution

Example:

public class TestClass {
    private int a = 5;
    {
        a = 10; 
    }

    private static int b = 10;

    static {
        b = 15;
    }

    public TestClass() {
        System.out.println(a + ", " + b);
        a = 20;
        b = 30;
        System.out.println(a + ", " + b);
    }

    public static void main(String args[]) {
        TestClass t = new TestClass();
    }
}

Output:

10, 15
20, 30
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
  • Not correct. Constructor execution includes calling `super()` and the variable and anonymous instance initializers, and then the remainder of its own code. – user207421 Feb 16 '16 at 00:53
  • Not really: in case we have a base class the chain will be: 1. base class static init (bot members and sections); 2. current class static init; 3. base class instance members init (booth members and sections); 4. base class constructor; 5. current class instant members init; 6. current class constructor. – Sergey Tychina Feb 16 '16 at 03:21
  • Again not correct. Calling `super()` elides everything about initializing the base class, but to elaborate it: (a) call the current class's constructor, whose first action is to call the base class's constructor, (b) call the base class's `super()`, (c) call the base class's instance variable and anonymous initializers, (d) call the rest of the base class's constructor, (e) return from the base class's constructor into the current class's constructor, (f) call the current class's instance variable and anonymous initializers, ... – user207421 Feb 16 '16 at 10:12
  • In terms of bytecode - yes, you are correct. all initialization happens in (constructor). My explanation was not really clear. by constructor execution I meant "code" inside the constructor, but not the constructor call itself. – Sergey Tychina Feb 17 '16 at 23:38
-1

When JVM loads the class, it actually loads Test.class and allocates memory for static variables such as y. Any member variables such as x will only be initialized upon construction of object like new Test();

SomeDude
  • 13,876
  • 5
  • 21
  • 44
  • No. The values are initialized when the constructor runs, once per instance. – user207421 Feb 16 '16 at 00:51
  • Before constructor runs, when JVM loads the class, does JVM allocates memory for x, y? may be static variables only in this case y. – SomeDude Feb 16 '16 at 01:10
  • @EJP member variables such as x will be initialized only upon new Test() right? edited my answer. – SomeDude Feb 16 '16 at 01:13
  • Space for instance variables is allocated when `new` is called. – user207421 Feb 16 '16 at 01:24
  • By instance variable you mean member variable right? That I agree. But static variables such as y and also static blocks inside a class are initialized when JVM loads the class or may be just before the class is ready to be constructed. – SomeDude Feb 16 '16 at 01:41
  • Wrong. I meant what I said. The term used by both me and the JLS is 'Instance variable'. 'Member variable' is not an acceptable alternative, as it includes static members. – user207421 Feb 16 '16 at 05:40
  • I am not here to argue. I really want to know how the static variables are initialized, See [here](http://beginnersbook.com/2013/05/static-variable/). It says : Static variables are initialized when class is loaded. Static variables in a class are initialized before any object of that class can be created. Static variables in a class are initialized before any static method of the class runs. Please point me to right resource if I am wrong. – SomeDude Feb 16 '16 at 14:37
  • You can't cite beginners' books here. I refer you to the JLS section cited in my answer. – user207421 Feb 22 '16 at 23:13
-2

When you declare a variable using an assignment statement like that, the value becomes the default value for the variable. If you don't change it, it will always return that value. If you do overwrite the default value, it will return the new one instead

Trash Can
  • 6,608
  • 5
  • 24
  • 38