aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code in curly braces of  class can be run?why? 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 in curly braces of  class can be run?why?" Watch "code in curly braces of  class can be run?why?" New topic
Author

code in curly braces of class can be run?why?

Breeze Zhang
Greenhorn

Joined: May 28, 2003
Posts: 5
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?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
They are called initializers
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
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.


use the [CODE] tags - it makes it much easier for people to help you.
Breeze Zhang
Greenhorn

Joined: May 28, 2003
Posts: 5
I know! Thank you all!
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: code in curly braces of class can be run?why?