Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum and static variable in constructor

Access to static fields in enum constructor is forbidden by the compiler. The source code below works, it uses a static field:

public enum TrickyEnum {     TrickyEnum1, TrickyEnum2;      static int count;      TrickyEnum()     {         incrementCount();     }      private static void incrementCount()     {         count++;     }      public static void main(String... args)     {         System.out.println("Count: " + count);     } } 

Output:

Count: 2.

But the code below does not work despite there being very little difference:

public enum TrickyEnum {     TrickyEnum1, TrickyEnum2;      static int count;      TrickyEnum()     {         count++; //compiler error     }      public static void main(String... args)     {         System.out.println("Count: " + count);     } } 

From my search, people usually claim that the problem is due to the order in which static fields are initialized. But first example works, so why do Java developers forbid the second example? It should also work.

like image 249
Pawel Avatar asked Dec 10 '13 15:12

Pawel


People also ask

Can you use enum in constructor?

The constructor takes a string value as a parameter and assigns value to the variable pizzaSize . Since the constructor is private , we cannot access it from outside the class. However, we can use enum constants to call the constructor.

Can we use static variable in constructor?

If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.

Can enum have static fields?

But in case of Enums we cannot alter order of static fields. The first thing in enum must be the constants which are actually static final instances of enum type.

Does an enum need a constructor?

By default, enums don't require constructor definitions and their default values are always the string used in the declaration.


1 Answers

The compiler allows a call to a static function, because it is not smart enough to prohibit it: the legitimacy of the call cannot be deduced without looking into the body of the incrementCount method.

The reason this is prohibited becomes clear when you run the following code:

enum TrickyEnum {     TrickyEnum1, TrickyEnum2;      static int count = 123; // Added an initial value      TrickyEnum()     {         incrementCount();     }      private static void incrementCount()     {         count++;         System.out.println("Count: " + count);     }      public static void showCount()     {         System.out.println("Count: " + count);     } }  public static void main (String[] args) throws java.lang.Exception {     TrickyEnum te = TrickyEnum.TrickyEnum1;     TrickyEnum.showCount(); } 

This prints

1 2 123 

which is extremely confusing to a programmer reading your code: essentially, incrementCount does its modifications to the static field before it has been initialized.

Here is a demo of this code on ideone.

like image 78
Sergey Kalinichenko Avatar answered Sep 23 '22 03:09

Sergey Kalinichenko