Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what order are initializer block and variable definitions and etc. executed? (in java)

I have problem understanding the order in which initialization happens. this is the order I assumed:

*Once per 
    1. Static variable declaration
    2. Static block
*Once per object
    3. variable declaration
    4. initialization block
    5. constructor

but according to this code I am obviously wrong:

    class SomethingWrongWithMe
    {
        {
            b=0;         //no. no error here.
            int a = b;   //Error: Cannot reference a field before it is defined.
        }
        int b = 0;
    }

And the error would disappear if I do this:

    class SomethingWrongWithMe
    {
        int b = 0;
        {
            b=0;
            int a = b;   //The error is gone.
        }
    }

I can't figure out why isn't there an error on

    b=0;
like image 391
Untitled Avatar asked Mar 12 '12 10:03

Untitled


People also ask

What is the order of execution in Java?

Order of execution When you have all the three in one class, the static blocks are executed first, followed by constructors and then the instance methods.

In which order are initialization blocks run?

Initialization blocks run in the same order in which they appear in the program. Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces.

What is executed first in Java?

The static blocks always execute first before the main() method in Java because the compiler stores them in memory at the time of class loading and before the object creation. Here, the compiler executes all the static blocks first, and after finishing the static block execution, it invokes the main() method.

In which order are instance variables initialized?

In Java, the order for initialization statements is as follows: static variables and static initializers in order. instance variables and instance initializers in order. constructors.


2 Answers

The Java Language Specification (section 8.3.2.3) says you can use a variable on the left hand side of an expression, i.e. assign to it, before it is declared, but you cannot use it on the right hand side.

All variables are initialized to their default values, then explicit initializers and anonymous blocks are run in the order they are found in the source file. Finally the constructor is called.

Statics are only run once on the first use of a class.

The compile error appears to be a rule of Java rather than something that necessarily makes sense in every case.

like image 166
Simon G. Avatar answered Nov 11 '22 10:11

Simon G.


Variable definitions are not done "before" blocks. They are both done at the same time, in the order that they are defined

class SomethingWrongWithMe {
    {
        b = debug("block 1");
    }
    int b = debug("define");
    {
        b = debug("block 2");
    }
    private int debug(String str) {
        System.out.println(str);
        return 0;
    }
}

Output

block 1
define
block 2
like image 35
Adam Avatar answered Nov 11 '22 10:11

Adam