Originally posted by Jesper Young:
DayLight ISIMA, please check your private messages for an administrative JavaRanch matter. You can see them by clicking My Private Messages.
Also, please use code tags when you post code.
About your question: There's an error, you put a ) in the wrong place. Did you try the code out? That's the quickest and easiest way to find out if it works.
[ July 11, 2008: Message edited by: Jesper Young ]
Originally posted by Jarek Jankowski:
For sure this code works. You can access a static member of a class by a reference to it (DOG, CAT and FISH are public, static and final instances of Animals class). I hope this helps.
[ July 11, 2008: Message edited by: Jarek Jankowski ]
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Ralph Jaus:
Hi Zheng,
generally, while studying for SCJP, it's better not to use an IDE but a simple text editor like notepad, etc. and to compile manually by javac.
Now to your problem: The code really works, how Jarek explained. The compiler generates from the enum the following class: <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="java"><font size="2">class Animals extends Enum{
public static final Animals Dog;
public static final Animals Cat;
....
}</font></pre><hr></blockquote>So "a.Dog" just references the (static) member variable "Dog" of class Animal. Of couse "sound" must not be private in your example.
[ July 11, 2008: Message edited by: Ralph Jaus ]
That should work.public static void main(String... args){
System.out.println(a.Dog);
Animals b = a.Dog;
}
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Ralph Jaus:
"a.Dog;" is the problem (how the compiler explained). What should it be ?
Use for example: That will work.
[ July 11, 2008: Message edited by: Ralph Jaus ]
[ July 11, 2008: Message edited by: Ralph Jaus ]
Thanks, Sharad<br />SCJP 5 (July, 2008)
Vipun Reddy
[My Blog]
All roads lead to JavaRanch
Ship Is Safe at Shore,But its not Built For That
Regards
vinod SCJP 6.0
Zheng Ren wrote:Hello everyone,
I've just bought the <SCJP study guide for java 6> and have finished the self test of chapter 1. I think there's an error for question 4 :
<blockquote>code:
<pre class="java">
enum Animals{DOG("woof"), CAT("meow", FISH("burble");....}
Class TestEnum{
static Animals a;
public static void main(...){
System.out.println(a.DOG.sound+" "+a.FISH.sound);
}
}
</pre>
</blockquote>
the answer is this code will work?! I don't think we can access a static field like this(it should be Animals.DOG no?)
[ July 11, 2008: Message edited by: Zheng ]
Ralph Jaus wrote:Hi Zheng,
generally, while studying for SCJP, it's better not to use an IDE but a simple text editor like notepad,
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|