• Post Reply Bookmark Topic Watch Topic
  • New Topic

code called before constructor  RSS feed

 
varun mathur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UseCodeTags when you post code snaps! Have look on here
 
varun mathur
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
varun mathur wrote: Abhiraman actually ....


BTW, Who is this?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extremely sorry for wrongly spelling out your name Abhimaran.
i was just referring to post topic when i wrote "java free block".
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!