File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Block...

 
Pankaj Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Question : if i remove the comment of Target A. But even though i have not commented Target B, it compiles perfectly. So what is goes on Here?
Can anybody explain me?

 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it compiles?

You can't have a statement outside of a block or method.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Pankaj,

You think, if you uncomment the line target1, and if it was
permitted, when would it get executed?

Think about static initializer too.
Execution order of static initializer, block code, constructor is
fixed.
Block is executed just before constructor in the order they appear
in the class definition.

Inside the class, but outside the block, only initialization can be
done; and yeah any method can be called too that returns something
to be assigned to the member variable.
A method that returns void is not permitted here;


Think about bold line again.


Regards,
cmbhatt
 
Pankaj Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, but you had mis-interpreted my question. My question is, if i uncomment the Target A, the program does not compile. Why? Because the statement inside block is compiling succeessfully.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have a statement outside of a block or method.
 
Pankaj Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I accept that i can't have any stetement outside block. But i want to know, is, what is reason that allow you a statement to be used inside a block? I mean how, i can't use outside, but can use inside without any effort. By writing the statement inside the block, what extra care jvm does for those statement?
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pankaj Patel:
Ok. I accept that i can't have any stetement outside block. But i want to know, is, what is reason that allow you a statement to be used inside a block? I mean how, i can't use outside, but can use inside without any effort. By writing the statement inside the block, what extra care jvm does for those statement?


In your example, that code "inside a block" is a instance initializer. It is defined to run during the initialization of an instance of the class.

It is not what "extra care jvm does" but what to actually do. What do you think is supposed to happen with the code?

Henry
 
Pankaj Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry Wong.

Code "inside a block" is a instance initializer. It is defined to run during the initialization of an instance of the class. So sir, accoring to you i can't use statement System.out.println("Outside a Block") in a static block. Becuause static block is going to be executed only once when the class is loaded into jvm and that is why it is not instance initializer.

Am i right? (I know i am wrong, but i like to know the reason so that i get very clear).


Thanks you very much in advance for your efforts.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pankaj,

You didn't interpret what I said in my previous post. I talked about static
initializer block too.


So sir, accoring to you i can't use statement System.out.println("Outside a Block") in a static block.


Why can't?
To make yourself sure that static initializer block has run once class is loaded you can put the print() statement in the static block too, that execute once when the class is first loaded.



Regards,
cmbhatt
[ April 26, 2007: Message edited by: Chandra Bhatt ]
 
Pankaj Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra Bhatt.
You are right. I have not interpreted completely your post. But now i am 100% clear about the logic.

Thanks you very much Everybody.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to that..


Block is executed just before constructor in the order they appear
in the class definition.


Block is executed after the call to the super constructor, if any, and before the completion of the current class' constructor. correct?



[ April 26, 2007: Message edited by: M Krishnan ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic