This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&K book, error on page 80?

 
Jorn Hansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
hardikjava shah
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We already spotted that one way back when.
 
Jorn Hansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic