• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Variable V/S Static Block

 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the following code :



I get the following output:
Initialized Called->false
Inside Main->false 1
Initialized Called->false
2

Can anyone explain me the output???
Thanks in advance!!
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehhh...Does not look like a java Script question to me...
Interesting question though...Pretty tricky
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on HTML and JavaScript.

For more information, please read this.

This post has been moved to a more appropriate forum.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initializers are called in the order that they occur. So in your code, the first thing that happens is a static block that calls a method, that assigns a boolean value to true. Then, after you make the assignment to true you get to the static initializer for the boolean variable and re-assign it to false.

In this case, the static block occurs before the variable initializer because it is written above the variable in the code. If the code looked like this:

You would get different results.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Sahu wrote: (In Duplicate Thread)
Thanks for your reply.
But I really want to know what happens when the static block calls any static function with assignments within it &
when main method calls up the static method ......
I actually what to know what happens to assignments??
I am a bit confused!!!


> what happens when the static block calls any static function with assignments within it
The static block calls the static function, and the static function makes its assignments. Then, when the static block comes to an end the next line of code is executed.

> (what happens) when main method calls up the static method
The same thing that happens when any method calls up a static method. If the class whose method is being called hasn't been initialized yet, then run through the static initialization processes. Then, once that is done, execute the code in the static method. Then continue on with the next line of code.

> I actually what to know what happens to assignments?
They occur in the order in which they are reached. In your example, the order they occur in is:
1) sum = 1 (code line 22. in original post, traced back to line 05.[static block])
2) initialized = true (code line 23. in original post, traced back to line 05.[static block])
3) initialized = false (code line 16. in original post)
4) sum = 2 (code line 22. in original post, traced back to line 12.[getSum()] from line 33.[main()])
5) initialized = true (code line 23. in original post, traced back to line 12.[getSum()] from line 33.[main()])

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic