This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum overloaded constructer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum overloaded constructer" Watch "Enum overloaded constructer" New topic

Enum overloaded constructer

Ameen khan
Ranch Hand

Joined: Jun 10, 2007
Posts: 52
can anybody give me an example of overloaded constructers in enum and how to use its enum value

SCJP 5.0<br />Next-> I Don't Know
Ameen khan
Ranch Hand

Joined: Jun 10, 2007
Posts: 52
enum CoffeeSize {

// the arguments after the enum value are "passed"
// as values to the constructor

CoffeeSize(int ounces) {

this.ounces = ounces; // assign the value to
// an instance variable

private int ounces; // an instance variable each enum
// value has
public int getOunces() {
return ounces;

class Coffee {
CoffeeSize size; // each instance of Coffee has-a
// CoffeeSize enum

public static void main(String[] args) {
Coffee drink1 = new Coffee();
drinkl.size = CoffeeSize.BIG;//
//how to assign second overloaded form for BIG(9,"a")

Coffee drink2 = new Coffee();
drink2.size = CoffeeSize. OVERWHELMING;

System.out.println(drinkl.size.getOunces()); // prints 8
System.out.println(drink2.size. getOunces()); // prints 16

[ August 06, 2007: Message edited by: Ameen khan ]
[ August 06, 2007: Message edited by: Ameen khan ]
Ansar Shah

Joined: May 02, 2007
Posts: 29

Here is the example for you.

And the output is as follows:

MANGO season is spring ,null
DATES season is spring ,winter
ORANGE season is winter ,null

Hope it help!

It is sorta covered in the JavaRanch Style Guide.
subject: Enum overloaded constructer
Similar Threads
Understanding enum bodies
Enum doubt
retrieve enumeration datatype into jsp
plz explain oveloading of constructor in enum with example
How to define constants for several classes?