Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code within a curly braces

 
Lohit Hosur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sunitha thejaswi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I left out the print method for the test1 class. here it is :
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic