Java Enum why static line executes only once?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Java Enum why static line executes only once?



The result is:
1
3
1
3
1
3
2



The constructor runs for A,B and for C (3 times). But if you use static keyword it runs only once. What is the reason of this? And why does this line executes last?


enum Enums
{
A, B, C;

{
System.out.println(1);
}

static
{
System.out.println(2);
}

private Enums()
{
System.out.println(3);
}
}

public class MainClass
{
public static void main(String args)
{
Enum en = Enums.C;
}
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name