• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Static Variable V/S Static Block

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ehhh...Does not look like a java Script question to me...
Interesting question though...Pretty tricky
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()])

 
We noticed he had no friends. So we gave him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic