aspose file tools
The moose likes Java in General and the fly likes enum constructor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "enum constructor problem" Watch "enum constructor problem" New topic
Author

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

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


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: enum constructor problem
 
Similar Threads
Interface question....
Are instances of enums static by default?
please explain the code and please anyone give example of overloading in enum
enum problem
Static object