Enum constructor
Consider the code below. I am reffering to some books/tutorials which say Constructor for Apple is called once for each constant when variable ap is defined. But I don't see this happens actually,i.e. When I comment out the the forloop after line (Apple ap), I don't get the print message from the constructor of the apple, which according to theory should happen. It's only when I invoke any method(ie. in for loop) on Apple I get print statments for all the enum constants.
So my questions in when is the constructor for each enum constalled called. Is it when the variable of enum decleared or when any method on the enum is executed?
enum Apple {
H(10), L(2), P(2), T(20);
private int price;
Apple(int p){
System.out.println(this);
price=p;
}
public int getprice(){return price;}
}
public class EnumTest {
public static void main(String[] args) {
Apple ap;
for(Apple a: Apple.values())
System.out.println(a+"-> "+a.getprice());
}
}
-SD.

