Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
java :: enum as main
ENUM X => static final class X extends java.lang.Enum
ENUM X will have
instances seperated by commas (optional semi colon at the end)
[optional private modifier] Contructor with optional parameters
values() method which will return X[]
name(), toString() methods will return the name of specific enum
(ex: In the above example SINGLETON.name() == SINGLETON.toString() == "SINGLETON")
instace and class variables/methods as usual in a class
ENUM X can only override toString() method
ENUM X can implement any interfaces
ENUM X can have main() method
ENUM X can have abstract method
ENUM X is serializable because java.lang.Enum implements java.io.Serializable interface
ENUM X have compareTo() method because java.lang.Enum implements java.io.Comparable interface
ENUM X's instances (constants) can be compared for equality using `==` or instance.equal() methods
// enum as Main
public enum Main {
SINGLETON(System.currentTimeMillis());
private long timeStamp;
Main(long timeStamp) {
this.timeStamp = timeStamp;
}
public long getTimeStamp() {
return timeStamp;
}
public void run() {
System.out.println(getTimeStamp());
}
public static void main(String[] tcs) {
SINGLETON.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.