• 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

Code execution...

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Consider the code
class vari{static int x=1;
static {System.out.println("value of x in vari's static block is "+x);}
{System.out.println("assigning "+(x=2));}
}
class vari1 extends vari{
static int x=3;
static {System.out.println("value of x in vari1's static block is "+x);}
{System.out.println("Now value of x is "+x);}
public static void main(String d[]){
new vari1();
}
}
-----------
outcome:
value of x in vari's static block is 1
value of x in vari1's static block is 3
assigning 2
Now value of x is 3
-----
As output shows I assume that order of code execution is
=> static variables initialization and then static initializer block execution in super class.
=> static variables initialization and then static initializer block execution in sub class.
=> instance initializer blocks execution in super and then in sub class
The way I think about the code execution is correct...or something else goes on here..?
Also I think last line of output should be 2 instead of 3 because x just has been assigned a value of 2 in super's instance
initializer block...

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nasir Khan:

As output shows I assume that order of code execution is
=> static variables initialization and then static initializer block execution in super class. <pre> slightly missed </pre>
=> static variables initialization and then static initializer block execution in sub class. <pre> slightly missed </pre>
=> instance initializer blocks execution in super and then in sub class <pre> anah </pre>
The way I think about the code execution is correct...or something else goes on here..?
Also I think last line of output should be 2 instead of 3 because x just has been assigned a value of 2 in super's instance
initializer block...



[This message has been edited by naveen sahu (edited December 28, 2000).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nasir,
The last line is 3 vs 2 because the superclass initializer is called before the subclass initializer or constructors. At that point in the compile the superclass is totally unaware of the fields which exist in the the subclass. The field 'x' in the superclass is assigned the value 3; not the field 'x' in the subclass.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited December 28, 2000).]
 
naveen sahu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Hi Nasir,
The last line is 3 vs 2 because the superclass initializer is called before the subclass initializer or constructors.
<pre>
the line you have written is true in conclusion but that associated reason is wrong.not have any link with explaination.
</pre>
At that point in the compile the superclass is totally unaware of the fields which exist in the the subclass.
<pre> true </pre>
The field 'x' in the superclass is assigned the value 3; not the field 'x' in the subclass.
<pre>
that is absolutely wrong . </pre>
<pre>
in the original code the variable x is declared in both of the classes. so second one hides first one.and rest of all done due to flow of execution. i can explain in detail but i don't want to
west my valuable time in this particular thread.better i searched some sesnsible thread.
</pre>


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic