• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing Instance Members  RSS feed

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initializing Instance Members

Sun talks about two alternatives to initializing instance members instead of using a constructor.

Alternative 1: Initializer block
Alternative 2: protected final method

The webpage says that the Java compiler copies initializer blocks into every constructor. How is this an alternative approach? In what order are initializer blocks executed? Before static initializer blocks?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leo donahue:
Initializing Instance Members

Sun talks about two alternatives to initializing instance members instead of using a constructor.

Alternative 1: Initializer block
Alternative 2: protected final method

The webpage says that the Java compiler copies initializer blocks into every constructor. How is this an alternative approach? In what order are initializer blocks executed? Before static initializer blocks?


If a class has no constructor, a default constructor is created.

Initializer blocks are executed from top to bottom in a class definition each time an instance of the class is created.

The static initializer blocks are executed from top to bottom only when the class is initialized.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll add that those initializers are inserted into the constructor after the call to super(), and before the rest of the constructor body. If one constructor calls another, then they're only inserted into the "base" constructor.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would the java compiler copy non-static intialization blocks into every constructor? The article didn't specify default constructors vs. supplied constructors.

I modified the code from the article, but I still don't understand what the article means by "copying initializer blocks into every constructor".

In the example, the static blocks execute in order, before the regular initialization block and before the stuff in the constructor.


The static initializer blocks are executed from top to bottom only when the class is initialized.

This is cool, a class can run itself without a main method??



[ November 30, 2006: Message edited by: leo donahue ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leo donahue:

I still don't understand what the article means by "copying initializer blocks into every constructor".


Just what it says. The initializer block isn't compiled directly into a method of any kind in the class file the way static blocks are (static initializer blocks are compiled into a static method named "<clinit>", and the JVM calls this method to initialize a class.) Instance initializer blocks are simply copied into each constructor, as you can see here. This class uses three different techniques for initializing instance variables:



If I disassemble it using "javap -c Foo", you see that all three initializations end up looking exactly the same, and they're all in the constructor. Note how the instance initializers come before the constructor code we wrote ourselves, but after the superclass constructor call:





This is cool, a class can run itself without a main method??


Yes. This is a favorite SCJP mock exam question. "What does this class do when you run it?" Any code in the static blocks runs before the launcher tries (and fails) to find main(). If the static init block contains a call to System.exit(), the launcher will exit before it ever even finds out there's no main() and so no error message will be produced.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

This is the most coolest thing I've ever seen.

javap....where have you been all my life!
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a good hard-copy book to learn how to read Java Byte Code for Java 5.0?

-- Kaydell
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!