• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing Instance Members

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

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

javap....where have you been all my life!
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic