aspose file tools*
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
Author

Code within a curly braces

Lohit Hosur
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Hi,
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:
static
{

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

Thanks,
Lohit


Lohit Hosur<br />SCJP 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

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.

Henry


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

Joined: Nov 14, 2005
Posts: 31
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
(static)
static:
{

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

}

and
(instance)

{

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,

sunitha
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.8.7.1.
InstanceInitializer:

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: http://aspose.com/file-tools
 
subject: Code within a curly braces
 
Similar Threads
basic question about static initializer block
Curly braces
Doubt with static initializers
Is this valid?
Anonymous class issues