This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Block... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Block..." Watch "Regarding Block..." New topic
Author

Regarding Block...

Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
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

Joined: Feb 07, 2005
Posts: 2367
Are you sure it compiles?

You can't have a statement outside of a block or method.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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


cmbhatt
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
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

Joined: Feb 07, 2005
Posts: 2367
You can't have a statement outside of a block or method.
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pankaj Patel
Ranch Hand

Joined: Jul 13, 2006
Posts: 73
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Jul 13, 2006
Posts: 73
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

Joined: Aug 13, 2006
Posts: 178
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Block...
 
Similar Threads
what is the difference between static variable and static block
static block and empty block inside any class.
Block execution
Static