Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static block is not being called

Tags:

java

final

Who can explain what's going on?

public class MagicFinal {

    public static void main(String[] args) {
        System.out.println(A.s);
    }
}

class A {
    static {
        System.out.println("class has been loaded");
    }

    public static final String s = "final";

    public static final Integer i = 3;
    
        
}

Console :

final

What's it? I don't understand why the class has not been loaded, I know classes always load at the first call. Field s is in pool of string, I see that final modifier is magic.

If I delete final modifier (public static String s = "final" ) I will get

Console :

class has been loaded

final

Note: I have changed field i : public static final int i = 3; and show it in console. I got the same as in String situation. Why?

like image 516
idmitriev Avatar asked Sep 24 '13 10:09

idmitriev


People also ask

How do you call a static block method?

Static block call your method only once at time of class creation, If you want to call method at time of class creation you can call it. Static block is only way by which you can call your static methods at time of class creation.

Can we call static block in java?

You can't call static block. It is called when Class object is created. You can think of it as constructor of Class object that describes your class. Unlike constructor which is called when instance is created.

What is a collection of static block is called?

Output. static block called Constructor called Constructor called. A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

Will static block be executed with final variable?

We can initialize a final static variable at the time of declaration. Initialize inside a static block : We can also initialize a final static variable inside a static block because we should initialize a final static variable before class and we know that static block is executed before main() method.


2 Answers

"final" is a string literal and as such is a compile-time constant expression. The value of a static final variable initialized with a compile-time constant expression is directly hardcoded into the class which references it, and no reference is made to the originating class. Therefore the initialization of the originating class does not occur.

As a side point, please note the distinction between class loading and class initialization: only the latter's occurrence is precisely specified by the JLS. Class loading can happen at any time.

like image 152
Marko Topolnik Avatar answered Oct 24 '22 04:10

Marko Topolnik


This is what is written in Java Language Specification {8.3.2.1 Initializers for Class Variables}. This must answer your question

One subtlety here is that, at run time, static variables that are final and that are initialized with compile-time constant values are initialized first. This also applies to such fields in interfaces (§9.3.1). These variables are “constants” that will never be observed to have their default initial values (§4.12.5), even by devious programs. See §12.4.2 and §13.4.9 for more discussion.

like image 22
Abhijith Nagarajan Avatar answered Oct 24 '22 05:10

Abhijith Nagarajan