File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Enums (super call) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enums (super call)" Watch "Enums (super call)" New topic
Author

Enums (super call)

Jonathan Storey
Greenhorn

Joined: May 14, 2007
Posts: 7
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,
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I don't see where you define the ounces() method.

When you add a body to an enum constant, you create an anonymous subclass of the enum type. That is why super is appropriate.
Jonathan Storey
Greenhorn

Joined: May 14, 2007
Posts: 7
Sorry, the brackets where left in by error. The code should have been

BIG(2){public int getOunces(){return super.ounces*3;}},


The question is really, why if I do the above does the code work as expected but if I leave off the 'super' part, I get an error 'non-static variable ounces cannot be referenced from a static context' and why adding 'super' resolves this issue?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
My question is: why does super.ounces *not* raise a compile time error? After all, ounces is private, and therefore shouldn't be accessible through the super reference.

It might actually be a bug in the Eclipse compiler. Did you try to compile the same with the Sun compiler? What version of Eclipse are you using?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jonathan Storey
Greenhorn

Joined: May 14, 2007
Posts: 7
We tried it from a command prompt using Java 1.6 compiler on Linux and we got the same error?

Eclipse SDK Version: 3.2.0 running JDK 1.5.07
[ May 15, 2007: Message edited by: Jonathan Storey ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I've tested this with Sun's JDK 1.5.0_11, and it also works.

The Java Language Specification doesn't say a lot about constant-specific class bodies because they state that they think the use will be rare.

Consider this example.



If you examine the bytecode produced when Judges is compiled, you see the following methods.



Notice that special method called access$102.

It appears that this method is what is used to actually change the value of the variables in the enum type.

Look at the bytecode produced from one of the constant bodies.



It appears that there is a special treatment of the keyword super here to access variables and methods in the enum type.

Note that if you try to just use the keyword super by itself within the constant-specific class body, you get a compile-time error, whereas in a normal class definition, super can be used by itself.

As an example, if I change the first constant-specific class body to

,

I get the following syntax error.


[ May 15, 2007: Message edited by: Keith Lynn ]
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
Actually, 'specialized enum constants' is implemented by anonymous local class declared in static initializer ( i.e static{} ).

If you omit super in super.ounces, compiler treats it like this:



Although anonymous class has its own ounces field, compiler interpretes as CoffeeSize3.this.ounces, and complains about that: you use instance field in static method.
[ May 15, 2007: Message edited by: Bupjae Lee ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enums (super call)
 
Similar Threads
use of " this " ...
Question about enums and enhanced for
Enum programs are not compiling in java5.0 compiler?