I have a confusion here.
If I cannot declare a class as static, how does enum works?
It looks like a static class, because it gets instantiated itself and can be called anywhere.
Looks like I can use enum almost like other static fields.
Is it safe to use enum?
enum types are automatically static. You can't have a non-static enum, so the static is implied.
JLS 8.9:
Nested enum types are implicitly static. It is permissible to explicitly declare a nested enum type to be static.
Each enum which you are declaring inside an enum type is static member of your enum type.And the type of your enum variables is the type of your enum,in short they are self typed.
Related
This question already has answers here:
Are instances of enums static by default?
(2 answers)
Closed 7 months ago.
I have been recently studying java enums and couldn't understand why they are implicitly public and static by nature. final I understand because they are constants but why the other tags?
If you would look at Java documentation, it is clearly mentioned that:
Programmers can not invoke constructors of enum.
which basically means that we can not create any object of enum using the keyword new. Now if enums weren't static, then how would we access them without any instance/object?
enum Color { RED, GREEN, BLUE; } // enum declaration
Color.RED //accessing enum constant
If you have noticed only have to access enum constants is through enum name (similar to how we access static members of any class).
So to be able to access enum constants without any object we need them to be static.
And enums are by default public so that we can freely access them anywhere however this is not a necessity we can use private or protected modifiers as well.
I have been recently studying Java enums and couldn't understand why they are implicitly public and static by nature.
final I understand because they are constants but why the other tags?
It is complicated, but I think you have some of the facts incorrect there.
According to the Java 17 Language Specification (JLS 8.9)
"It is a compile-time error if an enum declaration has the modifier abstract, final, sealed, or non-sealed."
See below.
"A NESTED enum class is implicitly static. That is, every member enum class and local enum class is static."
And non-nested classes are implicitly static too.
"It is a compile-time error if ... an enum declaration has MORE THAN ONE OF the access modifiers public, protected, and private (§6.6)."
But that is the same as any other class. This doesn't say that it is implicitly public.
"An enum class is either implicitly final OR implicitly sealed ..."
There is something rather subtle going on here. If an enum constant has a class body, then it actually defines an anonymous subclass of the enum class. In this case, the enum class is not final in the sense of "having no subclasses"
So:
Enum classes are NOT implicitly public. They can be private, for example.
Enum classes MAY BE implicitly final in the "has no subclasses" sense. But you were using final in the "constantness" sense. The binding between an enum constant name and the corresponding value cannot change; i.e. it is implicitly final in that sense.
However, the enum values can have mutable fields, so they are not necessarily constant in the sense that 42 is a constant. Just like you can change the array content with the following "constant":
final int[] CONST = new int[]{1, 2, 3};
Enum classes ARE implicitly static in contexts where another class could be non-static.
Why are they implicitly static? Well if they weren't, what would it mean? An implicitly static enum is effectively a set of singleton values. But it it wasn't, then each time you created an instance of the class that enclosed the enum class, you would be creating a new set of enum values. They are no longer singleton. This would be most unexpected ... and I am finding it hard to see how it would be useful.
It is known that Interface doesn't need constructor because all the data members of interface are public,static and final. Similarly enum also has all its constants as public static and final then how come it needs/had a constructor?
An interface cannot be instantiated, an enum can (and in fact will be, as each of its members is an instance of the enum itself).
Java's inner classes can be static or non-static. Non-static inner classes are tied to an instance of the enclosing class.
Annotations are a type of Java interface, and like any other class, they can be defined inside a class. Similarly, they can be declared static or non-static. What is the difference between these two choices, is there any difference between how they're used by consuming code, and is there any scenario where it'd make sense to use one or the other?
Example:
public class AnnotationContainer {
public static #interface StaticAnnotation {}
public #interface NonstaticAnnotation {}
}
No difference at all. Nested interfaces are always static.
This is described in JLS Sec 8.5.1 (for classes):
A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.
and JLS Sec 9.5 (for interfaces):
A member type declaration in an interface is implicitly public and static. It is permitted to redundantly specify either or both of these modifiers.
To expand a bit on Andy's correct answer that they are exactly the same because they are a special kind of interface declaration and "member interfaces" are implicitly static anyway:
JLS 10 9.6. Annotation Types:
An annotation type declaration specifies a new annotation type, a special kind of interface type. To distinguish an annotation type declaration from a normal interface declaration, the keyword interface is preceded by an at-sign (#).
JLS 10 8.5.1. Static Member Type Declarations :
A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.
and JLS 10 9.1.1. Interface Modifiers
The modifier static pertains only to member interfaces (§8.5.1, §9.5), not to top level interfaces (§7.6).
Side note: interestingly, these Chapters do not use the term "nested interface" defined at the top of Chapter 9, but it seems to be a synonym for "member interface":
JLS 10 Chapter 9. Interfaces:
A nested interface is any interface whose declaration occurs within the body of another class or interface.
For an enumeration defined in a class, like
class OuterClass {
public enum Method {
GET,
PUT,
POST,
DELETE;
}
}
Is the enumeration a static nested class (https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)? It seems to be the case judging from the syntax used to refer to it. Or is it a non-static nested class (an inner class)?
The JLS says
An enum declaration specifies a new enum type, a special kind of class type.
So it looks like the word from Oracle is that enums are classes.
If you declare an enum inside another class, then yes, it's an inner class. And enums are always static, so yes, it's fair to call an enum a static inner class (or nested class) when it's declared in another class.
As per §8.9 of the JLS:
An enum declaration specifies a new enum type, a special kind of class type.
[...]
A nested enum type is implicitly static. It is permitted for the declaration of a nested enum type to redundantly specify the static modifier. [...]
The generated bytecode for the enum declaration is as follows:
// compiled from: OuterClass.java
public final static enum INNERCLASS ...
So yes, enum is a static nested class in this case - confirmation in the JLS.
public interface Proposal {
public static final enum STATUS {
NEW ,
START ,
CONTINUE ,
SENTTOCLIENT
};
}
Java does not allow an enum to be final inside an interface, but by default every data member inside an interface is public static final. Can anybody clarify this?
Java does not allow you to create a class that extends an enum type. Therefore, enums themselves are always final, so using the final keyword is superfluous.
Of course, in a sense, enums are not final because you can define an anonymous subclass for each field inside of the enum descriptor. But it wouldn't make much sense to use the final keyword to prevent those types of descriptions, because people would have to create these subclasses within the same .java file, and anybody with rights to do that could just as easily remove the final keyword. There's no risk of someone extending your enum in some other package.
An enum can't be final, because the compiler will generate subclasses for each enum entry that the programmer has explicitly defined an implementation for.
Moreover, an enum where no instances have their own class body is implicitly final, by JLS section 8.9.
Two things:
enums are final subclasses of java.lang.Enum
if an enum is a member of a class, it is implicitly static
No point in declaring enum final. Final for classes means that they can not be inherited. However, enums can not be inherited by default (that is they are final).
The final thing is valid only for variables. However you should think of the enums more like data types than variables.