aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes B&K book, error on page 80? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "B&K book, error on page 80?" Watch "B&K book, error on page 80?" New topic
Author

B&K book, error on page 80?

Jorn Hansen
Greenhorn

Joined: Dec 13, 2006
Posts: 4
I am currently reading

SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) Sierra & Bates 0072253606 making use of the errata list on
http://www.mhprofessional.com/downloads/products/0072253606/0072253606_errata0504.txt.

I have a problem understanding the self test: Question 8 of chapter 1. Page 76. In my eyes the following code should fail. The static variable a has not been initialized. But according to the book it should compile and run without problems. What is correct? I have tried to compile it. And it compiles and runs successfully, but I don't understand why?



And one more thing: Class TestEnum is not declared public, but the JVM is able to run it anyway?

Hope someone can help me clear up my misunderstandings.


/Jorn Hansen, mainly Oracle Developer
hardikjava shah
Greenhorn

Joined: Dec 12, 2005
Posts: 13
TestEnum class need not be public for program to run.

Also, variable a is static member. Only instance local variables need to be initialized.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,


Joern wrote:
In my eyes the following code should fail. The static variable a has not been initialized.
(...)
Class TestEnum is not declared public, but the JVM is able to run it anyway?


Hi Joern, seems to be your first posting, so
Welcome on the Ranch!




The program prints "woof burble".
As Hardikjava wrote, only local variables have to be initialized.
By the way, there is no such a thing like an "instance local variable". Local variables are neither instance nor class variables. Just local.
Class and instance variables are automatically assigned to a default value. This is always 0 or 0.0 for primitives and null for objects, also null for enums.

The output of the program would be the same if you said:
static Animals a=Animals.CAT;
But then, you could use the variable directly:
Code altered:
prints:
woof burble
meow
null


By the way, I use Eclipse for Java coding.
It gives me a warning in the line:
System.out.println(a.DOG.sound + " " + a.FISH.sound);
complaining about me, I should use a static variable in a static way, I'd better say:
System.out.println(Animals.DOG.sound + " " + Animals.FISH.sound);


about public classes:
I have heard this once and again that people think, the class with the starting main method must be public, which is not true.

Maybe (I don't know) this comes from environments other than Eclipse, that don't allow non-public classes as starting points? But surely, this is not a requirement in Java.


Yours,
Bu.


---
and thanks to Joern for using the tag in his first posting!


all events occur in real time
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
We already spotted that one way back when.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jorn Hansen
Greenhorn

Joined: Dec 13, 2006
Posts: 4
Thank you all for your answers and heartly welcome to the Javaranch.

You have helped me a lot, but why are you allowed to reference DOG (a.DOG) when a == null. Why doesn't it give a NullPointerException?

About public in public static void main: Well, I have heard that reflection can access any methods and fields of an instance. Also the private ones. So it sounds reasonable that the public declaration of a main()-method is not strictly necessary.

[ December 14, 2006: Message edited by: Joern Hansen ]
[ December 14, 2006: Message edited by: Joern Hansen ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Joern asked:
Why doesn't it give a NullPointerException?


Because the enum elements of the enums are implicitely static.
You won't get a NullPointerException from a static variable. Being from an enum or a normal class:
e.g.
First prints zero (with the static variable) and then throws a NullPointerException.



and:
So it sounds reasonable that the public declaration of a main()-method is not strictly necessary.


It is necessary for the JVM to start the class.
This
compiles happily, but when you try to start it:
prompt> java Smurf
JVM laconically tells you that
"main method is not public."


Yours,
Bu.
Jorn Hansen
Greenhorn

Joined: Dec 13, 2006
Posts: 4
OK - a static enumeration field is initialized to a value of null.

But you can still reference other references through the variable, so a.DOG gives meaning and is the same as Animals.DOG.

And the clarification of the main() method means: The main()-method needs to be defined as public. But the class containing the main()-method needs not.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Joern wrote:
But you can still reference other references through the variable, so a.DOG gives meaning and is the same as Animals.DOG.

Yes. Only the latter is more appropriate.
By the way you can also concatenate theses references (legal but really ugly):


Because the output line was already ugly, I thought I can easily make it even uglier with the main method declaration.
Compiles without warning and prints:
burble

And Joern also wrote:
And the clarification of the main() method means: The main()-method needs to be defined as public. But the class containing the main()-method needs not.


needs not to compile and run


Yours,
Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B&K book, error on page 80?