• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static and Non static blocks  RSS feed

 
Ramesh Pappala
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is static block ? When it is executed ? How many times it will be called? Where it will be useful ?

What is non static block ? When it is executed ?

Give me an example on both
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static block will be call only once and it will be the first time your are accessing the class.

The non-static block will be call every time your are creating a new instance and it will be call just before the Constructor.

here's a example and it's output


output :
Static block of Class A
Non-Static block of a instance of Class A
Constructing object of type A
Non-Static block of a instance of Class A
Constructing object of type A
Static block of Class B
Non-Static block of a instance of Class B
Constructing object of type A
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This came up in the last few days in this thread. You may want to check it out for further discussion on the topic. If anything is unclear, feel free to come back and post a follow-up question.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!