This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Regarding Block...

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure it compiles?

You can't have a statement outside of a block or method.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have a statement outside of a block or method.
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic