This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes enum constructor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "enum constructor problem" Watch "enum constructor problem" New topic

enum constructor problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

enum Animals {
DOG("woof"), CAT("meow"), FISH("burble");
String sound;
Animals(String s) { sound = s; } ///should'nt this.sound be used here
class TestEnum {
static Animals a;
public static void main(String [] args) {
System.out.println(a.DOG.sound + " " + a.FISH.sound);

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

Please QuoteYourSources and UseCodeTags. And next time, write your question out of the code.

[My Blog]
All roads lead to JavaRanch
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

mohitkumar gupta wrote:should'nt this.sound be used here

No. "this." is only required if it otherwise would cause a name clash. If you had renamed "s" to "sound" it would be required. Now the compiler sees that "sound" can only mean its instance field.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: enum constructor problem
Similar Threads
Interface question....
Static object
please explain the code and please anyone give example of overloading in enum
Are instances of enums static by default?
enum problem