• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Enum

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

Please help me to understand the meaning of line //1 and line //2 in the following code.

enum CoffeeSize
{
BIG(8), HUGE(10),OVERWHELMING(16);
}

CoffeeSize(int ounce)
{
this.ounce=ounce;
}

private int ounce;
public int getounce()
{
return ounce;
}
}

class Coffee
{
CoffeeSize size; //1

public static void main(String args[])
{
Coffee drink=new Coffee();
drink.size=CoffeeSize.BIG; //2

Coffee drink1=new Coffee();
drink.size= CoffeeSize.OVERWHELMING;

System.out.println(drink.size.getounce());
System.out.println(drink1.size.getounce());
}
}

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

Your code gives compiler error due following:
1- extra parenthesis after
BIG(8), HUGE(10),OVERWHELMING(16);


Modified code:



static CoffeeSize size; //1
In the class Coffee, you have an enum CoffeeSize that is instance variable
of class Coffee.




Coffee drink=new Coffee();
drink.size=CoffeeSize.BIG; //2

You created an instance of your class in the main method and assigned
constant CoffeeSize.BIG to drink.size (member variable of Object drink)
CoffeeSize.BIG is enum constant, that can only be assigned to enum type object.





Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra,

Can anybody explain me, why the output is differ in the following two situations.

1)

The output is: 16 16


2)

The output is: 10 16

Thanks in Advance.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,

Recheck your output.
Nothing like what you say.
Output will be same...



Regards,
cmbhatt
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Chandra, I got it.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the initial code:
it seems that you re-assign the same object...
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Folks for your help but I also wants to know that what the "size" denoting in the code(at //1).



[ April 16, 2007: Message edited by: Gaurav Pavan Kumar Jain ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav,

 
Do not threaten THIS beaver! Not even with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic