• 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

Question related to Enum

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I have 2 questions related to following code
1. How can I access sum(int a , int b) method from main() method?
2. Why I can't make sum method static?

class EnumTest {
static enum test {
FIRSTCLASS("This is First Class") {
String performance() {
return "nisha";
}

// Can we add methods like this in one particular enum - FIRSTCLASS ma add karyu ne aevu..???
void sum(int a, int b) {
final int c = a + b;
System.out.println("this is sum: " + c);
}
};
public String toString() {
return str;
}

test() {

}

test(String s) {
str = s;
}

String str;

abstract String performance();
}

public static void main(String[] args) {

System.out.println(EnumTest.test.FIRSTCLASS);
}
}
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone reply to this?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For #1: I don't think you can, unless you put an abstract "sum" method into the enum itself. You don't have access to the anonymous classes that implement the individual enum values.

For #2, the error message should be pretty clear -- inner classes can't have static methods. The instance classes of an enum are inner classes.

Finally, note that English is the official language of JavaRanch; if you post anything in another language, you must provide a translation (I can't tell if (and if so, why) you repeated the same thing in two languages above.)
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all apologize to use some foreign language words in the code. It just happened that I pasted the code from one of my ongoing stuff and I had that weired comment in the code. Next time onwards will take care of it.

But going back to discussion:
I though enums are converted in to static inner class if you have a constant specific body. This means that previous code will get converted in following:

class EnumTest {
static class test {
static class FIRSTCLASS {

// Other code over here..
}
}
}
So if I try to make sum method static. Ideally it should allow. Let me know if my understanding regarding conversion of enums mentioned above is wrong.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you're correct about the value classes being static classes. Note, though, that FIRSTCLASS isn't the name of a value class; the value class is an anonymous class (it ends up in a file named EnumTest$test$1.class). FIRSTCLASS is just the name of a public final static variable in EnumTest.test that holds an instance of the anonymous class; the type of that variable is EnumTest.test . So FIRSTCLASS.sum() wouldn't find the method even if it could be defined, and maybe that's why the compiler won't let you define the method in the first place.
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very true.Just to add what Friedman is saying,

Above mentioned example got internally converted into
class EnumTest
{
abstract static class test
{
static final EnumTest.test FIRSTCLASS=new EnumTest.test("This is First Class")
{
String performance()
{
return "nisha";
}

void sum(int a, int b)
{
final int c = a + b;
System.out.println("this is sum: " + c);
}
};
public String toString()
{
return str;
}

test() {}

test(String s) {str = s;}
String str;

abstract String performance();
}

public static void main(String[] args) {

System.out.println(EnumTest.test.FIRSTCLASS);
}
}

this means when I want to use sum method using EnumTest.test.FIRSTCLASS.sum(1,1) it is actually trying to access child object using parent reference. It won't be allowed as a method on child object can be executed using parent reference only if that method is available in parent class. In this case sum is not declared in static class and that is why you can not access it.

Thanks Friedman,
It actually clear many doubts about enums for me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic