• Post Reply Bookmark Topic Watch Topic
  • New Topic

enums: how to call the method ret()  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since college is an enum, you can't call it with a constructor:


Instead you use the name of the enum:


Note that in Java, class names start with uppercase by convention so college should be College. And enum constants are all uppercase so arm should be ARM.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to being sure to follow the Java naming conventions that Jeanne pointed out, always be sure to properly indent your code. Both conventions are important aspects of coding that while not needed to make the code run, are not optional.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might also be worth pointing out that your ret() method (which should probably be public) appears to duplicate a facility already provided for all enums - ie, returning a unique ID (or "index").

And that method is called ordinal().

HIH

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:It might also be worth pointing out that your ret() method (which should probably be public) appears to duplicate a facility already provided for all enums - ie, returning a unique ID (or "index").

I'm being a bit pedantic here but they are not quite the same. The ordinal() method returns a unique value over which you have no control and which may well change over time for example if new enum constants are inserted, whereas the ret() method will always return the same value for a given enum constant. Admittedly in most cases this make no difference but if you use the unique value for persistence pruposes then you could have problems if you use ordinal().
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I'm being a bit pedantic here but they are not quite the same...

Very true. I was simply going by what I saw in the example.

Winston
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it is not possible to instantiated for enums !!!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:why it is not possible to instantiated for enums !!!

Because the enum IS the instantiation. Basically, an enum is a set of singleton objects, so they're instantiated when they're declared.

HIH

Winston
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i delcare instance inside main method its showing an error but wen ouside the main it does not show error
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

enum college
{

svce(1),srm(2),vit(3);

college(int a)
{
this.a=a;
}
private int a;
int ret()
{
return a;
}
}




public class Ennum
{
college k;
public static void main(String... a)
{
//college k; creates a problem
Ennum e=new Ennum();
System.out.println(e.k.svce.ret());


}
}

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:when i delcare instance inside main method its showing an error but wen ouside the main it does not show error

I think I've already explained: You DON'T "create" enums; they're already created by the definition.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!