Help coderanch get a
new server
by contributing to the fundraiser
    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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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:
  • Report post to moderator
When I run the following code :


class cache
{
static{
initializeIfNecessary();
}


static int sum;

public static int getSum() {
initializeIfNecessary();
return sum;
}

static boolean initialized = false;

private static void initializeIfNecessary()
{
System.out.println("Initialized Called->"+initialized);
if(!initialized) {

sum+=1;
initialized = true;

}
}
}

class client {
public static void main(String args[])
{
System.out.println("Inside Main->"+cache.initialized +" "+ cache.sum);
System.out.println(cache.getSum());
}
}




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: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
my eyes

Please format the code so its readable!!
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is because of the order of initialization of static members - blocks, variables etc.
The cache class gets loaded and all of its static code is executed at that time. Static initializers are executed at the same time that static variables are initialized. The initializations occur in lexical order.

So this gives your first output line:

Initialized Called->false



Over here, the first thing is the static block that calls the static method initializeIfNecessary() which has reference to the 2 variables, declared static. So default values are taken here and code is executed. In the second step it goes for next static member -> sum, for which the values has already been calculated.
Then comes the boolean member and its value is rewritten to false.
If you omit out this assignment of false, you will see that value - true set during initializeIfNecessary() is retained.

Hence your other 2 outputs.


 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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!!!
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Duplicate. Continuing here.
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
    Bookmark Topic Watch Topic
  • New Topic