wood burning stoves*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 4367
    
    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: 4367
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class blocks won't compile