• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static confusion from Head First into Java book  RSS feed

 
Philip Stephenson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am really enjoying going through this book but it seems they have some typos in dome of the excercises which is frustrating.

On page 310 they have this code in a "Be the Compiler". On page 312 they say the StaticSuper constructor just needs () in its signature, which is straightforward. However they don't mention any other changes needed for it to compile.

I am getting 2 errorsCannot define static initializer in inner type StaticSuper.StaticTestsStaticSuper.javaphilline 16November 1, 2005 12:14:24 PM422

and

SeverityDescriptionResourceIn FolderLocationCreation TimeId
2No enclosing instance of type StaticSuper is accessible. Must qualify the allocation with an enclosing instance of type StaticSuper (e.g. x.new A() where x is an instance of StaticSuper).StaticSuper.javaphilline 33November 1, 2005 12:14:24 PM424

Can you please give me an insight into what are the do's/don't with static in this case? When this happens at the end of chapters it has you questioning the principles you think you have understood from the rest of the chapter.

Thanks, Phil.


public class StaticSuper {

static {
System.out.println("super static block");
}

StaticSuper() {
System.out.println("super constructor");
}

public class StaticTests extends StaticSuper {

static int rand;

static {
rand = (int) (Math.random() * 6);
System.out.println("static block " + rand);
}

StaticTests() {
System.out.println("constructor");
}


}

public static void main(String[] args) {
System.out.println("in main");
StaticTests st = new StaticTests();
}

}
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re typo's: check the errata for the 2nd edition. Might help on other issues:

http://www.oreilly.com/catalog/hfjava2/errata/
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing the code didn't want to test statics in inner classes (if it did then ignore my post). With 2 public classes we need 2 java source files:



When it runs it produces:

super static block
static block 2
in main
super constructor
constructor


Which shows static initialization when the class is loaded and instance construction when the object is created. Hope this was inline with what the book was going through.
 
Philip Stephenson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks Ray, that works. I appreciate it.
 
Ben Polaski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I am not sure i am getting it right.The static methods are executed first.Is that right?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben

Static blocks get executed when the class is first loaded/initialised, before any constructor code.

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Polaski wrote:Hi.
I am not sure i am getting it right.The static methods are executed first.Is that right?

Static methods called first? No, methods are called when / in the order of being called. So if you called an instance method then called a static method, the static method would be called second.

But the static initializers are called when the classes are loaded, which happens before instances are created and initialized. So static initializers are called before constructors.
 
Ben Polaski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that out.Can you explain where the StaticSuper intializer is called?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Polaski wrote:Thanks for clearing that out.Can you explain where the StaticSuper intializer is called?

The static initializer is never explicitly called. It is executed when the class is initially loaded. It gets loaded basically the first time a reference to the class is made at run time. So in this example, the StaticTests class is loaded in order to call its main() method. Before the main() method can be called, the StaticTests class must be loaded and initialized. Because the StaticTests class has a super class, before it can be initialized, its super class must be loaded and initialized. So StaticTests initialization is put on hold and StaticSuper is loaded and initialized. Because StaticSuper also has a super class (Object), its initialization is put on hold until Object is done being loaded and initialized. Once Object is done being initialized, StaticSuper is initialized - and that means its static initializers are run (and its static variables are assigned values). When StaticSuper is done being initialized, StaticTests will be initialized and its static initializers will be run (and static variables assigned). All that gets done and then the main() method can get executed.

So it runs like this:

StaticTests.main() is called
-- StaticTests.class is loaded
---- StaticSuper.class is loaded
------ Object.class is loaded
------ Object.class is initialized
---- StaticSuper.class is initialized
-- StaticTests.class is initialized
StaticTests.main() is executed.
 
Ben Polaski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.Makes sense now.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even after more then 10 years, thank you for the clear explanation.

i forgot about the part where first the constructor "StaticSuper" is loaded first before the object "StaticTests is created.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!