File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class blocks won't compile

 
Rob Brew
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i right in thinking the class calls it when main is run?
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic