Class load time in Java

Hi members of stackoverflow, Here is a small question related to the actual value of "class load time".

For example, the following code:

public class Sequence {
    Sequence() {
        System.out.print("c ");
    }
    {
        System.out.print("y ");
    }
    public static void main(String[] args) {
        System.out.println("Indeed");
        new Sequence().go();
    }
    void go() {
        System.out.print("g ");
    }
    static { System.out.println("x "); }
} 

"x", , " ". , , ? , , , , "". - ? , , , ( ) , , , " ".

.

+5
5

JLS Chapter 12.4.1

T :

  • T - T.
  • T - , T, .
  • , T.
  • , T, (§4.12.4).
  • T - (§7.6), assert (§14.10), T (§8.1.3).

JLS Chapter 12.4.

12. , , . .

+3

main Sequence, JVM. , "x" - , .

+3

, run, . , . Sequence, x, Indeed, .

+3

It is not true that a static initialization block is executed during class loading. They are executed during class initialization . The exact point in time when the first one matters is not specified, while for the last there is a strict specification: it has the first access to the desktop of the class.

+2
source

The class Sequencemust be loaded before the method mainis invoked by the JVM, which leads to static initialization and, therefore, why xis the first thing to print.

+1
source

All Articles