Why static and instance init blocks in Enums behave differently than in classes

When I studied the Java test test, I found out that static initialization blocks are launched once when the class is loaded, in the order they appear in the source code, these initialization blocks of the instance are launched every time an instance is created, and this code in the constructors is run every time after the instance is created . To verify that I created a class with some static and instances of init blocks and a print constructor. Everything worked as expected, except that I thought that β€œloaded” only means at runtime, but I assume that this happens when the first instance is created, since I don't get any output at all if I don't create though would be 1 instance of the class. Then I tried the same with the listing, and the order was turned off. First of all,initialization blocks are run once for each value that enumerates when the enumeration first refers to the code, and secondly, the init blocks marked with static start after what I assumed were instance initialization blocks! This is the opposite of what I expected. Here is a breakdown of my questions.

  • Why do init blocks marked with static run continue in enumeration?
  • Can an enum have blocks of init instances?
  • Why are the blocks, which I thought are instances of init blocks, run only once when an enum is loaded, and not every time a new enum value is referenced?
  • The class of static initialization blocks is started when the class is loaded. What does sinker mean? Does this only happen once when an object is created in a class?

Thanks! This is very confusing for me.

public class EnumInit {
public static void main(String[] args) {
    System.out.println(Color.RED.toString() + " Main");
    MyInit myInit = new MyInit();
    System.out.println(Color.BLUE.toString() + " Main");
    MyInit mySecondInit = new MyInit();

}
}

enum Color {    
RED, BLUE, GREEN;
String instanceVar = "Enum Instance Variable Text";
static { System.out.println("Enum Static init block 1"); }
{ System.out.println("Enum Instance init block 1"); }
static { System.out.println("Enum Static static init block 2"); }
Color() { 
    System.out.println(instanceVar);
    System.out.println("Enum String Literal"); 
}
{ System.out.println("Enum Instance init block 2"); }   
}

class MyInit {
String instanceVar = "Class Instance Variable Text";
static { System.out.println("Class Static init block 1"); }
{ System.out.println("Class Instance init block 1"); }
static { System.out.println("Class Static static init block 2"); }
MyInit() { 
    System.out.println(instanceVar);
    System.out.println("Class String Literal"); 
}
{ System.out.println("Class Instance init block 2"); }  
}
+4
source share
1 answer

The Java language specification speaks of enumconstants

, E Enum, n, n E. , enum, - . , .

So

enum Color {    
    RED, BLUE, GREEN;
    ...
}

public static final Color RED = new Color();
public static final Color BLUE = new Color();
public static final Color GREEN = new Color();

static, .

init, , ?

. .

init-?

, , .

, , , init, , , , enum?

( ) . enum

Color color = Color.RED;

.

, . ? , ?

JVM , ClassLoader . .

+7

All Articles