jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes doubt with static block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "doubt with static block?" Watch "doubt with static block?" New topic

doubt with static block?

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Hi Everyone,

here i'm going to share one question about static block , actually when i wrote a class having static data member declaration and when tried to disassemble the code by using javap tool then i got something confusing ....let me first write a sample code then you guys easily able to aware about my issue..

here i'm writing one Test class which have a static data member declaration only..

and now here,after disassemble the code using by javap is...

here is fine i have no question regarding above but when i have bellow code.

then after disassembling the above code i found following.

it insert static block here so i'm a bit confused that's why i have a question in my mind that...
1- it is ok, if we do't write any constructor in a class then by default compiler insert a default constructor
2-so can we say here that compiler also insert a static block in the case of static data member declaration with assignment but when i decompile the code and i saw that compiler is not doing this thing
3-only in the above mention condition means if any class having static data member declaration ,disassembler shows the static block.
4-so what is mean here to insert static block and who is responsible for compiler or disassembler(javap)??
please clear my doubt with your valuable answers..
Manish Dubey J

Joined: Oct 08, 2011
Posts: 9

Hi Vivek,
I think it is obvious. Static blocks are used to initialize static members of class. It is the first thing to be executed, so when you write -- static int x=10; it is initialized with static block.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
You should not try to learn anything from decompiled code. Who know what relationship it will have to the original code.
I agree. Here's the link: http://aspose.com/file-tools
subject: doubt with static block?
It's not a secret anymore!