While reading my JSCP book, I starting playing about with enums. I wonder if anyone is able to answer this question.
//*****CODE SAMPLE********
class Coffee3 {
enum CoffeeSize3
{
BIG(2){public int getOunces(){return super.ounces()*3;}},
SMALL(1);
CoffeeSize3(int ounces)
{
this.ounces = ounces;
}
public int getOunces()
{
return ounces;
}
public void setOunces(int ounces)
{
this.ounces = ounces;
}
private int ounces;
}
CoffeeSize3 size;
}
public class TestEnums3
{
public static void main (
String[] args)
{
Coffee3 drink3 = new Coffee3();
drink3.size = Coffee3.CoffeeSize3.BIG;
//size = Coffee3.CoffeeSize3.BIG;
System.out.println(drink3.size.getOunces());
Coffee3.CoffeeSize3.BIG.setOunces(10);
//Coffee3.CoffeeSize3.BIG.ounces =22;
System.out.println(drink3.size.getOunces());
}
}
//******END CODE
Can someone explain to me why if I set the overriden 'getOunces()' method to be
BIG(2){public int getOunces(){return ounces()*3;}},
removing the 'super' call on that method the compiler complains with the error
Exception in
thread "main" java.lang.NullPointerException
at TestEnums3.main(TestEnums3.java:40)
And my eclipse session adds
Multiple markers at this line
- overrides Coffee3.CoffeeSize3.getOunces
- Cannot make a static reference to the non-static field ounces
But leaving in the super call on that variable returns the value I expected? Surely 'super' calls are reserved for sub/super classes?
Thanks,