Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Breeze Zhang
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are called initializers
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jon Dornback
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know! Thank you all!
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic