| Author |
code called before constructor
|
varun mathur
Greenhorn
Joined: Jul 07, 2010
Posts: 5
|
|
public class TestStaticBlock {
{
System.out.println("Varun");
}
public static void main(String[] args) {
new TestStaticBlock();
}
public TestStaticBlock() {
System.out.println("Sumit");
}
}
The output is:
Varun
Sumit
Can anyone please explain this program
|
 |
Ram Narayan.M
Ranch Hand
Joined: Jul 11, 2010
Posts: 213
|
|
Varun string printing block is called "Initialization Block" which is executed during each instance creation...
What happens is... in each constructor, super() method is first method call to be executed... After finishing this constructor execution, secondly, Initialization block will be executed...
So that only, after super() call, init block is executed first, so prints "Varun" and then followed by the other instructions in the constructor...
|
SCJP 6 [SCJP - Old is Gold]
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
UseCodeTags when you post code snaps! Have look on here
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
varun mathur
Greenhorn
Joined: Jul 07, 2010
Posts: 5
|
|
Thanks Ram
One more query . Is this initialization block is also known as static block.
If not then is there a thing such as static block?
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
There are instance initialization block and static initialization blocks! There are used for different uses! Try to find the usage with the meanings!
|
 |
varun mathur
Greenhorn
Joined: Jul 07, 2010
Posts: 5
|
|
Abhiraman actually i got a bit confused in naming.
I read your post of "java free blocks" and now I completely understand how this code works.
thanks
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
varun mathur wrote: Abhiraman actually ....
BTW, Who is this?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
|
|
|
What's a Java free block? I've never heard of it. What was shown in that thread was a static initialiser.
|
 |
varun mathur
Greenhorn
Joined: Jul 07, 2010
Posts: 5
|
|
Extremely sorry for wrongly spelling out your name Abhimaran.
i was just referring to post topic when i wrote "java free block".
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
|
|
|
As I said, it's not a free block, but an initialiser. Note there are two kinds of initialiser: static and instance. You can read about them in the Java™ Language Specification §8.3.2.1 and §8.3.2.2 and §8.6 and §8.7. It isn't easy to read, however.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Campbell Ritchie wrote:What's a Java free block? I've never heard of it.
Are there any thing like Java free floating blocks?
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 724
|
|
The closest thing to a "free-floating block" - purely syntactially - is an init-block, as included in the starting post of this thread.
It seems to be unrelated to any constructor or method, but it only seems that way. This becomes very clear when you dissasemble the byte code of a class file.
You'll find that the body of an init block is copied into every constructor, default or otherwise, immidiately after the call to super(), and before the rest of the constructor body. *
For example:
* Had the parameterized constructor included a call to this(), then the body of the init block and the initialization of someString would not have been copied (needlessly) into it.
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
|
|
Abimaran Kugathasan wrote: . . . Are there any thing like Java free floating blocks?
Look in the Java Language Specification. I couldn't find it there, and have never heard of it before.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
|
|
Jelle Klap wrote:The closest thing to a "free-floating block" . . . is an init-block . . .
As I said earlier, what was shown previously were initialiser blocks.
|
 |
 |
|
|
subject: code called before constructor
|
|
|