• 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

Doubt in ENUM

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
public class test
{
drink d;
public static void main(String a[])
{
test b=new test();
test c=new test();

b.d=drink.tea;
c.d=drink.tea;

System.out.println(b.d.getval());
System.out.println(c.d.getval());

}

}

enum drink
{
coffee(5),tea(6);

int val;
drink(int val){this.val=val;}
int getval(){return val++;}

}
O/p is 6,7

My doubt is that the in int val is nonstatic and the o/p should be 6,6
as for each assignment the value of the obejct is got from the enum declaration. And in my code there are two differnt object.

please help.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saravana.T kumar:
for each assignment the value of the obejct is got from the enum declaration. And in my code there are two differnt object.

As you can easily prove, there is only one object for each enum value.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Manfred said is correct,Let me add some more to that.
EAch enum member is actually a public static final field
so when you have a enum
enum drink { tea(1) , coffee(2)}
Here tea and coffee are actually "public static final" references to the object of type drink, And as tea [ or coffee] is static field two different references of test class "b and c" are ultimately referring to same drink object "tea" through d in test class and as a result the answer is not 6,6 and its 6,7.
Just remember each enum member [tea/coffee] is "public static final" .
Hope this adds to what Manfred says and makes the solution more clear to understand.
Thanks
Deepak
 
saravana.T kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if you say that it is 'public static final' than how can we dhange the value of a final variable???
can you please answer this???
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
Here tea and coffee are actually "public static final" references to the object of type drink



They are reference variables which refer to the object of type drink. So you can change the value of the Object (i.e., Object's state is changeable), but can't assign different object to them (illegal, since they are declared final).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic