• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which will be loaded first static variable or static block?  RSS feed

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my friend asked me that which will load first static variable or static block ?
My answer was to static variable.
. So he gave me two program and said to differentiate between them

1st program



Output of this :

0
90

2nd Program


Output of this :

90
90


I tried to decompile the byte code and found it's same for both the above equation. Please help me to differentiate between them. I am confiused when the static variable will initialised.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the section 12.4 of JLS explains it in detail with examples.

The relevant sub part for your question is section 12.4.2 - point 9 which states that...

12.4.2. Detailed Initialization Procedure


Something, something, something....

9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.



 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It goes to show how multiple initialisers can easily turn into bad design.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It goes to show how multiple initialisers can easily turn into bad design.


So does it mean loading of static variable and static block is unpredictable ,

Please someone tell me what is the exact answer or How do i assure my friend that static variables are loaded first ? as in many books it is written that static variables are loaded before static block
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heena Agarwal wrote:I think the section 12.4 of JLS explains it in detail with examples.



9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.





but heena then it will become similar as procedural language . Does it means that at some points java is a procedural language
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does it mean loading of static variable and static block is unpredictable

I don't see how that would follow from what Campbell said. What it means is that relying on one happening before the other leads to code that can confuse people who read it - and that's not good.

I tried to decompile the byte code and found it's same for both the above equation.

Interesting. Which decompiler did you use? I just tried it with Jode, and got different decompiled source codes.

then it will become similar as procedural language . Does it means that at some points java is a procedural language

I don't see how that follows at all. Can you elaborate why you think that?
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
Interesting. Which decompiler did you use? I just tried it with Jode, and got different decompiled source codes.

I used cavaj and it shows same

Ulf could you please tell me the exact answer with a little bit of explaination
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used javac and it shows same

javac is a compiler, not a decompiler.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops sorry my mistake it's cavaj .
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too bad, cavaj seems to be Windows only. But Jode produces different output for both, so that should help explain it. That, and of course what Heena quoted from the JLS.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj jain wrote: . . . So does it mean loading of static variable and static block is unpredictable . . .
No, it is actually very predictable, but the rules are complicated and it is easy to get them wrong and then you get nasty errors. As Ulf says, don't rely on one happening before the other.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!