• 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 in curly braces of class can be run?why?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Simple{
int i;
{
i = 10;
System.out.println("i = " + i);
}
public static void main(String[] args) {
new Simple();
}
}
--------------------------------------------
if remove "{}" of "i=10...", compile error,so
think the code in "{}" can be considerd as anonymous method which should never be called. But, the result print i = 10 , Who know java how to deal with this situation?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are called initializers
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are called blocks of code or as Alton said intializers. Try a few questions on them. Make sure you know when this/these block/s of code would be executed. For example guess (without executing ofcourse) what is the output of the program.

Now put new test(); before System.out.println("i = " + i); and guess the result.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also be aware that there is the static initializer. code in {} in a class will be executed for each instance of that class. however, if you put
static {/* code goes here*/}
in the class, that will only be executed once for the entire class. here's an example:

run that code, and then remove static from before the {} and see the difference. note that you can't access non-static variables from within a static initializer.
 
Breeze Zhang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know! Thank you all!
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
These are called blocks of code or as Alton said intializers. Try a few questions on them. Make sure you know when this/these block/s of code would be executed. For example guess (without executing ofcourse) what is the output of the program.

Now put new test(); before System.out.println("i = " + i); and guess the result.


I compiled the above code & did run.I had a doubt.Are these kinda initializers act like constructers.Coz they are invoked only when I instatntiate.If I don't they are not invoked.
please clarify.
Thanks
Veena
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Breeze.
The compiler gathers the code that initializes instance members, and block instance initializers in textual order -in order of appearance in the program text- and places them into the constructor; just after the call to the super constructor, but before the first statement written by the programmer in the constructor. In this way instance initializers are executed before the code within the constructor.
Using javap -c is possible to check this out.
reply
    Bookmark Topic Watch Topic
  • New Topic