Are java variable variables static?

public enum Operations { SINGLE, MULTIPLE; private Type operation; public void setOperation(Type operation) { this.operation = operation; } public Type getOperation() { return operation; } public static void main(String[] args) { Operations oper1 = Operations.SINGLE; oper1.setOperation(Type.GET); Operations oper2 = Operations.SINGLE; oper2.setOperation(Type.POST); System.out.println(oper1.getOperation()); System.out.println(oper2.getOperation()); } } enum Type { POST, GET; } 

In the above code, the operation value is changed for both operations. How can I have two instances of Operations.SINGLE with different types of operations?

+7
source share
6 answers

Yes, instances are implicitly static and final . This means that the code is unreasonable. Imagine two threads calling SINGLE.setOperation(Type) ; you will not trust what you call.

From the Java Language Specification, Section 8.9 :

Enumeration types (ยง8.9) must not be declared abstract; this will result in a compile time error.

An enumeration type is implicit final if it contains at least one enumeration constant that has the class body.

It is a compile-time error to explicitly declare an enum type final.

Nested enumeration types are implicitly static. It is permissible to explicitly declare a nested enumeration type static.

And in the next section:

An enumeration type body may contain enumeration constants. An enumeration constant defines an instance of an enumeration type.

Since there is only one instance of each enumeration constant, it is permissible to use the == operator instead of the equals method when comparing two object references, if it is known that at least one of them refers to the enumeration constant.

+12
source

How can I use two instances of Operations.SINGLE with different types of operations?

The main idea of enum is that there is one and only one instance of each of its members. This allows you to safely compare them for equality, without fear that another SINGLE or MULTIPLE will be created elsewhere.

If you want multiple instances of SINGLE , make it a class , not an enum . The fact that you made your enum indirectly modifiable points in one direction: using enum is the wrong choice in your situation.

+11
source

Enum instances are "static" (that is, they behave like static variables), but are not immutable .

All threads see the same object that the enumeration name refers to - they look like single games, with a guarantee from the JVM with iron that there is only one copy of the enumeration. Changing an enumeration field changes it for everyone.

Good practice is to make your fields final in the enumeration and make them immutable.

+10
source

Iโ€™ve been late a year and a half. But I see that I did not answer this question.

The solution will use a class instead of enum, which has these two enumerations as its fields:

 class Operation { Quantity quantity; Type type; Operation(Quantity quantity, Type type) { this.quantity = quantity; this.type = type; } } 

You can, of course, use an enumeration instead of a class. Then you will need to list all the combinations:

 enum Operation { SINGLE_GET(Quantity.SINGLE, Type.GET) SINGLE_POST(Quantity.SINGLE, Type.POST) MULTIPLE_GET(Quantity.MULTIPLE, Type.GET) // ... more options // contents same as in class Operation, constructor private by default } 

Both approaches are valid, sometimes you really want to list all the combinations most of the time, however you should probably stick with the class approach.

For brevity, I did not define the Quantity and Type enumerations, these are just simple enumerations.

+2
source

Yes, all enum elements are equal to static final constant . However, as mentioned in another darijan answer, there is a logical error in your program.

0
source

There is an error in the fourth line of the main method

 oper1.setOperation(Type.POST); 

it should be

 oper2.setOperation(Type.POST); 
0
source

All Articles