This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Non static initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non static initialization" Watch "Non static initialization" New topic
Author

Non static initialization

shailu kneni
Greenhorn

Joined: Feb 02, 2004
Posts: 7
What does it mean to put some code in a block? Please look at this code. What is the difference in putting the lines in bold in a block vs not putting them in a block?

Thanks,
Shails
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
The code in a block is an instance initializer block. It is executed each time an instance of the class is created. You can have any number of instance initializers. Instance initializers are executed after superclass construction but before the constructor of the current class is executed. Otherwise, they are executed in top-down declaration order.
Contrast with the idea of a static initializer block, which is executed when a class is loaded. A static initializer block looks the same as an instance initializer block except it is preceded with the word static.
One significant use for instance initialization blocks is to allow for initialization of classes that can't have constructors--anonymous inner classes.
-Jeff-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non static initialization
 
Similar Threads
two question about initialize object
bit shift, thread q's
How many object are eligible?
Number of Objects eligible for Garbage Collection
static initializer code blocks