Forums Register Login

Which will be loaded first static variable or static block?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.




+Pie Number of slices to send: Send
It goes to show how multiple initialisers can easily turn into bad design.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

I used javac and it shows same


javac is a compiler, not a decompiler.
+Pie Number of slices to send: Send
oops sorry my mistake it's cavaj .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 732 times.
Similar Threads
Class Variable access
Strange STATIC BLOCK behaviour
Really Really Confused
Static
Coding Tricks
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:13:21.