aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
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: 14688
    
  16

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: 19791
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum constructor problem