Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static enum vs. Non-static enum [duplicate]

What's the difference between static and non-static enum in Java? Both usages are same.

Is it correct that all static ones are loaded on memory on startup, and non-static ones are loaded on demand? If yes, then which method is better? Keeping some data always in memory or using server resources to load them each time?

public class Test {

    public enum Enum1 {
        A, B
    }

    public static enum Enum2 {
        C, D
    }

    public static void main(String[] args) {
        Enum1 a = Enum1.A;
        Enum1 b = Enum1.B;

        Enum2 c = Enum2.C;
        Enum2 d = Enum2.D;
    }
}
like image 405
AHHP Avatar asked Apr 17 '14 08:04

AHHP


People also ask

Can an enum be static?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Are enums static by default?

Yes, enums are effectively static.

Why do we use enumerations?

Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.

Can enum be static C++?

You can have a static variable of an enum type that holds a value. An enum is no variable. yes, but a variable that stores the state encoded in an enum is.


3 Answers

All enums are effectively static. If you have a nested enum, it is much the same as a static class.

All classes are lazily loaded (enums or otherwise) however when they are loaded they are loaded all at once. i.e. you can't have a few constants loaded but not others (except in the middle of class initialization)

Java allows certain modifiers to be implicit to avoid having to declare them all the time. This means that adding a modifier doesn't necessarily do anything other than provide a longer way of writing the same thing.

Default modifiers for

class field/method/nested class - package local, non-final, non-static

enum and nested enum - package local, final and static

interface field - public static final

interface method - public abstract

nested class in an interface - public static, non-final

Note: while static is optional for an enum it is always static. However, final cannot be set for an enum even though it is always notionally final (Technically you can have subclasses with overridden implementations for constants)

EDIT: The only place you need to use static with enum is with import static of an enum's value. Thank you @man910

like image 58
Peter Lawrey Avatar answered Oct 17 '22 02:10

Peter Lawrey


If you're talking about nested enums, they are implicitly static by default.

According to the Java Language Specification:

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

like image 31
Konstantin Yovkov Avatar answered Oct 17 '22 03:10

Konstantin Yovkov


All Enums are implicitly static, its just you don't need to write the static keyword. Similarly to the way all interface methods are implicitly public.

like image 14
Elliott Hill Avatar answered Oct 17 '22 01:10

Elliott Hill