Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constant Specific class body [ENUM]

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As we know that a constant specific class body will usually have a overriden method. For example in below code take from SCJP book, getLidCode() method is overriden in LARGE and EXTRA_LARGE constants. Notice that EXTRA_LARGE constant has another method defined getSpecial(), Can anyone tell me how to invoke getSpecial()?
enum CoffeeSizesWithOverriden {

SMALL(1), BIG(3), LARGE(5){
public String getLidCode() {
return "B";
}
}, EXTRA_LARGE(10){
public String getLidCode() {
return "C";
}
int special=10;
public int getSpecial() {
return special;
}
};

private int ounces;

CoffeeSizesWithOverriden(int ounces) {
this.ounces = ounces;
}

public int getOunces() {
return ounces;
}

public String getLidCode() {
return "A";
}
};

Thanks
Deepak
 
Murali Kakarla
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot invoke getSpecial() method unless it is defined in enclosing enum type.

Here is the notes from java lang spec 3.0 (page 250):

Instance methods declared in these class bodies are may be invoked outside the enclosing enum type only if they override accessible methods in the enclosing enum type.


Murali...
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!!
But as per the program given the method getSpecial()....is defined inside the enum body
EXTRA LARGE(),than why we cannot call the method getspecial()........
please if anyone could ellaborate and explain it will be kinda of you ..........


Thanks in advance.................
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhwani mathur:
than why we cannot call the method getspecial()
The overriding is implemented with anonymous inner classes. As result, it's impossible (without using the refelction API) to call methods that are not in the parent class.
[ July 13, 2007: Message edited by: Manfred Klug ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!!Thank you Manfred
for the explanation,but now i am bit confused with anonymous inner class ,is there a difference between anonymous inner class and normal inner class,if yes please can you explain?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhwani mathur:
is there a difference between anonymous inner class and normal inner class
There is no distinct type for anonymous inner classes. There is no way to tell the compiler that there is a class which extends Animal and has a method named hello.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well!!! Manfred what i understood from the below shown code is here
class Test is anonymous inner class please let me know if i am correct or not ?
but code i have made it bold just to ask one more question after method hello() there are empty braces but why have you used a semicolon is it syntax also the keyword static?


It will be kinda of you if you ellaborate and explain..........Thanks in advance.........
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhwani mathur:
is here class Test is anonymous inner class
Not Test, the instance of Animal is an anonymous inner class.

but why have you used a semicolon is it syntax also the keyword static?
Try it.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welll!!!Manfred definately i will try the above given code.........and let you know if again arrive on some doubts....anyways thanks for the explanation up to now..............
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys have diverted the topic to inner classes, Can we return back to enum and solve this question.
Please help
Thanks
Deepak
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Jain:
You guys have diverted the topic to inner classes, Can we return back to enum and solve this question.
Since the overriding is implemented with anonymous inner classes, they are the key for this question. If you are interested in the answer without internals, look at the post from Murali Kakarla.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic