• 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
  • 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 variables

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

I am writing my SCJP1.4 in 10 days . But I was surprised with the answer I got with the code below . This was one of the mock exam Q`s .


public class Practice implements I{
static int k = 1;
static{
k = k * 2;
}
{
k = k * 2;
}
public static void main(String args[]){
Practice t1 = new Practice();
Practice t2 = new Practice();
System.out.println(t1.k);
System.out.println(t2.k);
System.out.println(k);
}
}

interface I{
int k =1;
}


The O/P was :
8
8
8

I have no clue how we got this answer .

I would really appreciate anyone if anyone could explain me in detail .

Thanks in advance.
[ September 28, 2007: Message edited by: Rakesh Yelugoila ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I observe:

Outside of any methods in your class, there are two blocks. One labeled static and other unlabeled as anything. The unlabeled block is executed every time an object of this class is made. The static block is executed every time the class itself is invoked. In your program, the static block is invoked twice (when the program is first run and then when the initialization takes place every time).

I think this is how it is going:

1. When the class is being fired initially, the variable is getting initialized to 1 initially and then being set to 2 thru the static block.

2. At the time of instantiation again, the static block is first run and consequently the value is going to 4.

3. The final nail in the coffin is with the unlabeled block which literally acts like a constructor (terming it an initializer block is more appropriate) setting the value to 4*2 which is 8.

Good doubt this.. It makes one think..

By the way, were you deliberate in using a common variable name in several place?

Best Regards
Rahul :roll:
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me the following things happened

1. static block gets executed when the class gets loaded, so here k will be 2 when the class
gets loaded.

2. Instance block will get executed when an instance for tht class gets created. i,e in this example, Practice t1 = new Practice(); statement will cause the instance block {k=k*2}to gets executed(constructor will get called only after this), Since k is a static varable, it ll be common for all the obljects of Practice. Now, k will be k*2=>2*2=>4

3. Similarly when Practice t2 = new Practice(); gets executed, k will become 8.

4. And t1.k,t2.k ,k refers to the same static variable k, so it prints 8,8,8

Thank you!!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Out put i got is
before in static block k= 1
after in static block k= 2
before in NON static block k= 2
after in NON static block k= 4
Created t1..
before in NON static block k= 4
after in NON static block k= 8
Created t2..
8
8
8

it looks like the static block is executed only once so k =2
then the first instance creation will execute the non-static block making k = 2*2 = 4
then the second instance creation will execute the non-static block again making k = 4*2 = 8
so i1.k,i2.k and k (shadows the k variable in the interface,to print the value in the interface i think I.k should do it) will be 8
 
Rakesh Yelugoila
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all so much for this elaborate explaination . I really appreciate all your help.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>it looks like the static block is executed only once

Yes the static initialization block is executed only once no matter how many objects of the class you have.

Suppose you have a class type Test and you want to instantiate an object with the name 'myTest' using constructor myTest1=new Test(); In that case, overall process is as follows--

1) First the class file is loaded into JVM and right that time static block runs. Remember static block will run only once.

2) First a constructor of the superclass of the Test class is called and variables are initialized according to the code in the superclass constructor.In the superclass constructor, first thing done is invoking its superclass constructor and this process goes on and on till class Object's constructor is invoked.

3) Then code returns to the constructor of Test class.Before any other code in the test class constructor is invoked,the non-static initialization bloak in the code of Static class is invoked.

4) Suppose you want to create another object of class Test and you write Test myClass2=new Test(); In this case, steps 2 and 3 are run but not step #1.

So point to remember is static initialization block will be run only once the moment class file is loaded into JVM.Non-static initialization blocks will be run once for each object of the class created using new.There could be more than one static/non-static initialization blocks in the code.They are executed in the order in which they appear.

Hope this helps.

----Vishram
 
reply
    Bookmark Topic Watch Topic
  • New Topic