aspose file tools*
The moose likes Java in General and the fly likes Enum Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enum Problem" Watch "Enum Problem" New topic
Author

Enum Problem

Soumya Ranjan Mohanty
Ranch Hand

Joined: Mar 07, 2010
Posts: 44


Showing Compilation error as Incompatible type. Why?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

How does Coffee knows about CoffeeSize ?


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

The enum name is not CoffeeSize but CofTest.CoffeeSize -- it's a nested enum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carlos Hoces
Greenhorn

Joined: Mar 02, 2009
Posts: 2
class Coffee is not static, and you try to reference it from a static context: main.
If you make Coffee static, you'll have a clean compilation.
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
Carlos Hoces wrote:class Coffee is not static, and you try to reference it from a static context: main.
If you make Coffee static, you'll have a clean compilation.


That's wrong. Of course you can use non-static classes in a static context. That's what constructors are for... If you couldn't use non-static classes in a static context there wouldn't be any non-static classes. Rob's answer is right, nested enum => qualified name.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Christian Dillinger wrote:
Carlos Hoces wrote:class Coffee is not static, and you try to reference it from a static context: main.
If you make Coffee static, you'll have a clean compilation.


That's wrong. Of course you can use non-static classes in a static context. That's what constructors are for... If you couldn't use non-static classes in a static context there wouldn't be any non-static classes. Rob's answer is right, nested enum => qualified name.

Also, Coffee is static - because all nested enums are implicitly static.
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
Mike Simmons wrote:
Also, Coffee is static - because all nested enums are implicitly static.


But Coffee is not an enum. Coffee is a good old inner class.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Oops.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Coffee is a proper top-level class. It's located outside the CofTest class. It's not public no, but that doesn't change a thing.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Um, yes. I was Confusing it with CoffeeSize, which is a nested enum. It's not clear to me why the static-ness of Coffee was ever under discussion here, as it's a very simple class. I was imagining a more interesting topic than was actually there.
satish varma
Greenhorn

Joined: Feb 11, 2010
Posts: 27
Every thing is correct except one thing that is,

Whenever compiler invokes the above code the instance members of "Coffee" class will be instantiated and "CoffeeSize cs" is not with in that class (Coffee) scope, then compile time error raised

from line 15
here reference variable "cs" is declared in "coffee" class ,for this also compiler raised error.

to resolve this "CofTest" members make available to the "Coffee" class
for this simply "Coffee" class extends "CofTest" class. thats it

Every thing is fine in below code. enum class we can declare either outside of the class or inside of the class but not inside a method. Here dont compare enum with Inner classes, in above code enum class is CoffeeSize
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum Problem