• 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

Code within a curly braces

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic