File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code within a curly braces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code within a curly braces" Watch "Code within a curly braces" New topic

Code within a curly braces

Lohit Hosur

Joined: Oct 29, 2005
Posts: 6
I wanted to know the significance of declaring variables within a curly braces. Like this:
class Xyz {

int x;

int x=1;


What else can be written inside a pair of curlr braces?
And what happens if we put static, like this:

Also tell me any helpful links available for this in javaranch.
Pls, help me.


Lohit Hosur<br />SCJP 1.4
Henry Wong

Joined: Sep 28, 2004
Posts: 20514

The first case is called an instance initializer. It is executed when instance variables are initialize -- and done in order with instance variables. The second case is call a static initializer, it is executed when static variables are initialized -- and also done in order.

You can pretty much do anything you want, but you must remember that parts of the object may not be set yet.

For static initializers, static variables following the initializer is still set to the default value of null or zero. And static initializers following the initializers have not executed yet.

For instance initializers, instance variables following the initializer is still set to the default value of null or zero. Other instance initializers following the initializer have not executed yet. And while the constructor of the super class has executed, the constructor has not.

To be honest, I have found very few cases where the instance initializer has been useful.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sunitha thejaswi
Ranch Hand

Joined: Nov 14, 2005
Posts: 34
Hi Henry
could you please explain this is little more detail may be with an example.
When is this instance intializer block and static intializer block executed.
what happens if we have something like this

int i=89;
float f=1.2f;
int j,k;




int i=89;
float f=1.2f;
int j,k;


when is this executed how r the variable intialized.

please explain in more detail
thank you,

Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
Sunitha, Here is what the java Specification say about initilization of instance and static blocks.

8.6 Instance Initializers
An instance initializer declared in a class is executed when an instance of the class
is created (�15.9), as specified in �

8.7 Static Initializers
Any static initializers declared in a class are executed when the class is initialized
and, together with any field initializers (�8.3.2) for class variables, may be used to
initialize the class variables of the class (�12.4).

This came from the pdf version if you want to read it.

Also, all the varibles you declared in the static and instance initalizers have scope only within the block of the initalizer. In other words all the varibles you create in your examples can't be used outside of the block where you created them. example:
The following code will print 0 not 23. This because the foo varible declared in the initilizer block is a differnt varible then the one declared in the test1 class. Hoped this helped.

Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
Sorry, I left out the print method for the test1 class. here it is :
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
sunitha , Here is another example to show the order of how static and instance initilizer are executed. This example extends a class and print the order in which static, instance initilizer, and constructor are executed.

output of the program is :

Static test1 initilizer is executing
Static test2 initilizer is executing

Test1 instance initilizer is executing
Constructor test1 is executing

Instance initilizer test2 is executing
Constructor for test2 is executing

Heres the program.
Hope this help you. Thomas
I agree. Here's the link:
subject: Code within a curly braces
It's not a secret anymore!