1
class a
{
    static final  int a =5;
    static {
        System.out.println("hi");

    }
}
class b
{
    public static void main(String[] args) {
        System.out.println(a.a);
    }   

}

Why doesn't the static block run,and the output is only

5

whereas if I remove final keyword from the class variable, the static block gets executed and the output becomes

hi 
5
Roman C
  • 49,761
  • 33
  • 66
  • 176
user3239652
  • 775
  • 2
  • 9
  • 23
  • Once you assign a value to a primitive data type having the `final` modifier you can't change that value again, in other words you can't reassign the variable `a` to have a new value other than the int `5`. You'll get a compiler error if you try that. – Omoro May 01 '14 at 08:45
  • 1
    This is actually a good question whose title is a little misleading resulting in the downvotes. – geoand May 01 '14 at 08:50
  • @geoand so what edit should I make to the title? – user3239652 May 01 '14 at 08:52
  • @user3239652 One suggestion would be 'Does the combination of static and final modifiers result have any effect on static block execution'. Somthing along those lines, because the title you have now suggests to some people that you don't actually know what final does. – geoand May 01 '14 at 08:54
  • @user3239652 updated my answer, included the several ways the static block can be triggered – jantristanmilan May 01 '14 at 09:04

2 Answers2

5

Basically what happened is that the static final combination on primitives and Strings cause them to be inlined by the compiler, and this might have prevented the static initialization block from execution, since a class is never loaded by the classloader, as a.a was resolved during compilation

endriu_l
  • 1,649
  • 16
  • 20
4

Because your variables with the keywords static final are compiled-time constants which do not trigger the loading of the class containing the field.

Try putting running this code with static final on the variable a

System.out.println(a.a)
a var = new a();

as you can see the output will be

5
hi

The static block isnt triggered when a is called but the moment an instance of the class is created it gets triggered. It can be triggered by any of these:

  • an instance of the class is created,
  • a static method of the class is invoked,
  • a static field of the class is assigned,
  • a non-constant static field is used, or
  • for a top-level class, an assert statement lexically nested within the class is executed.

A question very similar to yours which might be helpful: Static block in Java not executed

Community
  • 1
  • 1
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
  • I don't think this is strictly correct. The `static`s should be initialized when the class is loaded and it should be being loaded by `b` referencing it. However, as @endriu_l explains in his answer, the compiler is inlining value of the field `a.a` in `b` and therefore the class `a` is never loaded meaning the `static` block is never executed – Nick Holt May 01 '14 at 09:11