Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Enum Problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Showing Compilation error as Incompatible type. Why?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does Coffee knows about CoffeeSize ?
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enum name is not CoffeeSize but CofTest.CoffeeSize -- it's a nested enum.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops.

Thanks.
 
Rob Spoor
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic