• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt with static block?

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
-----------------------------------------
vivek
 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not try to learn anything from decompiled code. Who know what relationship it will have to the original code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic