• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum Problem

 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Showing Compilation error as Incompatible type. Why?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does Coffee knows about CoffeeSize ?
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The enum name is not CoffeeSize but CofTest.CoffeeSize -- it's a nested enum.
 
Carlos Hoces
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.

Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic