Reputation: 848
class Ideone
{
enum Members{
A(1),B(2),C(3); // I have 3 enum constants here
private int size;
Members(int size){
//System.out.println("Initializing var with size = "+size);
}
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.print("main ");
// your code goes here
for (Members i: Members.values()){
System.out.print(i.name());
System.out.println(i.size);
}
}
}
Here goes the Output of the program-
main
A
0
B
0
C
0
Why is the size value not printed as 1, 2 and 3 instead of 0?
Upvotes: 0
Views: 56
Reputation: 12538
Because you did not map the size attribute. Change the enum constructor to:
Members(int size){
this.size = size;
}
Btw: you should always mark enum fields as final
as enums just exist once (singletons). So change private int size
to private final int size
Upvotes: 4