This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Class blocks won't compile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class blocks won Watch "Class blocks won New topic
Author

Class blocks won't compile

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Copied from the head first Java book, no idea why this won't compile:

StaticTests.java


probably something really obvious, any ideas folks?

Thanks,
Rob.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Generally speaking, it would help if you told us exactly what error you were getting. That gives us a clue where to look, and what to look for.

In this case, though - take a look at line 7. You're missing some brackets on the constructor.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
There was a typing error in the book, brackets in line 7 fixed that.

What i can't understand is why the static field in StaticSuper runs first. What is calling it?
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Am i right in thinking the class calls it when main is run?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Static initialization blocks - which is what you have beginning at lines 3 and 15 - are run when the class is loaded. So that's usually at the first point a reference is made to it. In this case, it will run before the main method does. The StaticTests class has to be loaded before any of its methods can be run, and that causes all its superclasses to be loaded as well.
 
wood burning stoves
 
subject: Class blocks won't compile
 
Similar Threads
Please clarify my doubts about static methods
Question on java.util.Random
please see the following code:
Does the usage of static prevents us from OO
Static confusion from Head First into Java book