This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static Variable V/S Static Block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Variable V/S Static Block" Watch "Static Variable V/S Static Block" New topic
Author

Static Variable V/S Static Block

Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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

Joined: Feb 08, 2009
Posts: 597

Ehhh...Does not look like a java Script question to me...
Interesting question though...Pretty tricky


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

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()])

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Variable V/S Static Block
 
Similar Threads
multiples project no output
Writing an output of a java application to a File.
BasicsDemo error message
Java Beginner
Static Variable V/S Static Block