• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion regardind enum and constructors

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
enum Coffesize
{
BIG(8),HUGE(10)

Coffeesize (int ounces)
{
this.ounces=ounces;
}
private in ounces;
public int getounces()
{
return ounces;
}
}
class Coffee
{
Coffesize size;
public Staic...........................
Coffe drink1=new Coffee();
drink1.size=Cofeesize.BIG;//this statement calls the constructor altough
integer is not passed how
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never call the destructor of an enum explicitly.

An enum is a kind of class, of which only a fixed number of instances exist: the constants that you defined in the enum. So in your case, there are two instances, referred to by BIG and HUGE.

You can re-write an enum as a regular class; maybe this will help you to understand what happens. It looks more or less like this:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it doesn't work. You are not calling its constructor, but its "ounces" attribute, which you have quite correctly given private access. Your app won't compile.
You can read all about enums here in the Java tutorial.

CR
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic