Is the main method used differently in lists and classes?
In the following code:
enum Rank {
FIRST(20), SECOND(0), THIRD(8);
Rank(int value) {
System.out.print(value);
}
public static void main (String[] args) {
System.out.println(" " + Rank.values().length);
}
}
This gives the following output:
2008 3
If, however, the method is main
declared in some other class like this:
class XYZ {
public static void main (String[] args) {
System.out.println("\n" + Rank.values().length);
}
}
Exit only 3
. What is the difference between main
enum and main
class? Why do I have two different outputs?
source to share
Continuum constants static
. They are initialized when the class is initialized (a enum
is just a class). This means that when you call a main
class that refers to a type enum
, all values ββare created using the constructor.
If the other class is not referenced in enum
any way, then the output will not be printed because it is not evaluated enum
.
source to share
The most significant difference between class and enumeration is that ENUM pre-defines its own instances. The value is printed while the enumeration is loaded, it executes the constructor for the Enum to create the ranks, which in turn print its value. while the execution of main for an ordinary class has no method implementation and no objects are created automatically. You need to create instances of the class. I hope you get my point.
More about Enum
So the difference is in the main method, but the difference is visible to you because the instances for the enum are created when the enum is loaded by the VM. :)
As requested by @Blip I am updating my answer, so even if you highlight the main method it won't change anything. Since the main method still refers to the Rank enum, hence the Enum is loaded by the runtime, and since we already know that Enums will be instantiated at class load time, it will still execute the constructor, and hence you will see output in 2008 3
source to share
You actually see a constructor for printing enums.
So in the beginning it Rank.FIRST
is created and System.out.print(value);
called from 20.
The same happens with Rank.SECOND
and Rank.THIRD
; all of this is done by the classloader because for the enumeration Rank is used EnumCreation
.
Because the constructor is Rank
using print
and not println
, they all appear on the same line.
Then when EnumCreation
it enters the main method, it prints a space followed by a Rank.values().length
newline.
When the XYZ
main method is introduced, it should do the same; but it will print a new line instead of space.
However, from a class point of view, you have 3 classes. Rank, EnumCreation and XYZ. Rank and EnumCreation just share 1 * .java file.
Hope this helps me figure it out!
source to share
There is a difference between an initializer Class
and an instance (or Object
) initializer . Class
initializers run on boot Class
, which usually happens the first time you access it. Object
initializers are constructors that only run when a new instance is created with a keyword new
(reflection of a ban).
Enum
are static instances. You can think of them as singles - you never new
a Enum
.
So let's say I have this definition Class
public class Foo {
static{
System.out.println("Someone loaded the class");
}
public Foo(){
System.out.print("Foo constructor");
}
public static void main(String[] args) {
System.out.println("Making Foo");
Foo foo = new Foo();
}
}
Output signal
Someone loaded the class Making foo Foo constructor
Enum
will work as a static block in Foo
.
Here's Enum
an example
public enum FooValues {
HELLO,GOODBYE,WHAT;
private FooValues(){
System.out.println(this);
}
public static void main(String[] args) {
System.out.println("in main");
}
}
This gives
HELLO GOODBYE WHAT in main
Because on load, Enum
all values ββare initialized during the load phase of the class and then the main
.
source to share