I wrote a test class and I was trying the enum type:
public class EnumTest {
enum Car{MERCEDEZ, FERRARI};
public static void main(String[] args) {
Car carObj;
carObj.run();
}
}
So it seems that I can 'declare' a class using the enum (because i do not have a class Car in my package), and I put the method run()
to see what happens. After that, i wrote the run()
body as the IDE adviced me, but inside the enum too.
enum Car {
MERCEDEZ, FERRARI;
public void run() {
}
};
I think that it is very weird, but ok. I continued with my Frankenstein experiment and tried to initialize the object with a constructor on main.
enum Car {
MERCEDEZ, FERRARI;
public void run() {
// TODO Auto-generated method stub
};
Car(){}
};
Car carObj = new Car();
And it says that I can not instantiate the type EnumTest.Car, so my test is finished, but with doubt. Sorry if it seems to be silly, but i did not find this on my searches.
Question: Why I can use the Car 'like' a class, but I can not initialize it?