public
Last active

java :: enum as main

  • Download Gist
X.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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();
}
}
points to note
1 2 3 4 5 6 7 8 9 10
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 implement any interfaces
ENUM X can have main() method

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.