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 overloaded constructer

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody give me an example of overloaded constructers in enum and how to use its enum value
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
e.g
enum CoffeeSize {

BIG(8), HUGE(10), OVERWHELMING(16);
BIG(9,"a");
// 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 ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

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!

Thanks
 
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