aspose file tools
The moose likes Java in General and the fly likes confusion regardind enum and constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "confusion regardind enum and constructors" Watch "confusion regardind enum and constructors" New topic
Author

confusion regardind enum and constructors

kapilg gupta
Greenhorn

Joined: Jun 14, 2006
Posts: 10
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
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: confusion regardind enum and constructors
 
Similar Threads
Question about enums and enhanced for
a variable is used before declaration in enum and its running ok how
use of " this " ...
Enums...
Enum programs are not compiling in java5.0 compiler?