public class A{ static int a = 1111; static { a = a-- - --a; } { a = a++ + ++a; } public static void main(String[] args) { System.out.println(a); } } 

Ask what the program will display. It is clear to me that if we have not created an instance of the class, then the class-level variables (static) will be initialized, and the static block will also be executed, even if an instance of the class is not created. And the answer is here 2. Because at the time of the call, only the static initialization block was executed. But my question is different. I thought there would be an NPE exception, since the variable does not exist yet. I thought so because I believed that the public static void main(String[] args) entry point is executed first, and then in order of initialization passes.

So at what point the static field and the stat were initialized. class А block?

  • What NPE can be on the primitive? - Roman Danilov
  • First, the entire class is loaded, including all static initialization, and only then can any methods be called - etki
  • And when did this primitive even create? aa .. it seems i understood. At first all classes are loaded and all static is initialized in the established order, i.e. first this class A, in it a stat. variable, then stat. block, and then the statistical method is created. So ? Otherwise, I thought that the entry point is a method, which means it is first created. - Turalllb
  • And what if there are two classes A and B and each has two stats. variable. I think they will be initialized stupidly in order. First class A and then B, as well as located in the folder .. But if the first variable in class A is assigned the value of a variable from class B, then the order is a little disturbed. First, class A, reached the first variable, got into class B, initialized its fields completely (or just the same static field?) And then returned and initialized the second variable of class A. It seems this is the subtleties of classLoader that you shouldn’t climb yet - Turalllb
  • @Turalllb Static class variables are initialized when the class is loaded. It is loaded when someone calls to it (creates an instance or refers to a static member). The class that contains the main method is accessed by the virtual machine when we start the program, specifying the class as an argument to it. If a class is not used by anyone, then it is not initialized. The order of the directory does not matter. - m. vokhm pm

1 answer 1

Class initialization

Before initialization, loading the necessary classes (ClassLoader, java.lang ... requires separate consideration)

Initialization order:

  1. The class initialization block is initialized first.
  2. Then initialization blocks are initialized.
  3. Then the public static void main (String [] args) method is called, which is the entry point to the application. It should be written according to the standard.
  4. And at the very end, the class constructor is called.

Code example:

  /* Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ класса ΠŸΠΎΡ€ΡΠ΄ΠΎΠΊ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ: 1. ΠŸΠ΅Ρ€Π²Ρ‹ΠΌ инициализируСтся статичСский Π±Π»ΠΎΠΊ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ класса 2. Π—Π°Ρ‚Π΅ΠΌ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΡŽΡ‚ΡΡ Π±Π»ΠΎΠΊΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ 3. Π—Π°Ρ‚Π΅ΠΌ вызываСтся ΠΌΠ΅Ρ‚ΠΎΠ΄ public static void main(String[] args), ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ являСтся Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ Π²Ρ…ΠΎΠ΄Π° Π² ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΈ Π² самом ΠΊΠΎΠ½Ρ†Π΅ вызываСтся конструктор класса *ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅.* 1. ΠœΠ΅Ρ‚ΠΎΠ΄, имя ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ совпадаСт с конструктором Π½Π΅ вызываСтся ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. 2. ΠœΠ΅Ρ‚ΠΎΠ΄, имя ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ main() Π² ΠΌΠΎΠ΅ΠΌ ΠΊΠΎΠ΄Π΅, Π½ΠΎ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½Π΅ являСтся Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ Π²Ρ…ΠΎΠ΄Π°, Π½Π΅ вызываСтся ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. НС являСтся ΠΎΠ½ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ Π²Ρ…ΠΎΠ΄Π°, ΠΏΠΎΡ‚ΠΎΠΌΡƒ ΠΊΠ°ΠΊ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ void ΠΈ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ². 3. ΠžΠ±Ρ‹Ρ‡Π½Ρ‹Π΅ статичСскиС ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π½Π΅ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ΡΡ ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. 4. ΠžΠ±Ρ‹Ρ‡Π½Ρ‹Π΅ нСстатичСскиС ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π½Π΅ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ΡΡ ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. */ package myInitClass; /** * * @author vvm */ public class InitClass { InitClass(){ // конструктор класса System.out.print(" ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ "); } { // Π±Π»ΠΎΠΊ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ System.out.print(" 3 "); } void InitClass(){// имя ΠΌΠ΅Ρ‚ΠΎΠ΄Π° совпадаСт с ΠΈΠΌΠ΅Π½Π΅ΠΌ конструктора Π½ΠΎ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ void // ΠΌΠ΅Ρ‚ΠΎΠ΄ Π½Π΅ вызываСтся ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ System.out.print(" ΠΌΠ΅Ρ‚ΠΎΠ΄ InitClass "); } public static void main(String[] args) { System.out.print(" 2 "); new InitClass(); } public void main(){ //- это ΠΎΠ±Ρ‹Ρ‡Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ с ΠΈΠΌΠ΅Π½Π΅ΠΌ main // имя ΠΌΠ΅Ρ‚ΠΎΠ΄Π° совпадаСт с ΠΈΠΌΠ΅Π½Π΅ΠΌ Ρ‚ΠΎΡ‡ΠΊΠΈ Π²Ρ…ΠΎΠ΄Π°, Π½ΠΎ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² // ΠΌΠ΅Ρ‚ΠΎΠ΄ Π½Π΅ вызываСтся ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ System.out.print(" ΠΌΠ΅Ρ‚ΠΎΠ΄ main - Π½Π΅ Ρ‚ΠΎΡ‡ΠΊΠ° Π²Ρ…ΠΎΠ΄Π° "); } public static void method(){ // статичСский ΠΌΠ΅Ρ‚ΠΎΠ΄ // ΠΌΠ΅Ρ‚ΠΎΠ΄ Π½Π΅ вызываСтся ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ System.out.print("статичныС ΠΌΠ΅Ρ‚ΠΎΠ΄ с ΠΈΠΌΠ΅Π½Π΅ΠΌ method()"); } static { // статичСский Π±Π»ΠΎΠΊ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ System.out.print(" 1 "); } } /* run: 1 2 3 ΠšΠΎΠ½ΡΡ‚Ρ€ΡƒΠΊΡ‚ΠΎΡ€ BUILD SUCCESSFUL (total time: 0 seconds) */ 
  • The code is clear, but they wrote everything very messy. I am familiar with the order of initialization, just for some reason, the public static void main(String[] args) method attached particular importance, he thought the very first one and initialized, since it is an entry point. It turns out it is initialized as a normal static method. - Turalllb
  • The public static void method main (String [] args) is really an entry point. If you write a public method void main () {} - then it will be a normal method and it will not be called during initialization. But before the entry point, initialization blocks are called. - Vyacheslav Mischenko