• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static initiliazer

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello evry body ,

i have find this code on a mock exam :

*********************

class Bird {
{ System.out.print("b1 "); }
public Bird() { System.out.print("b2 "); }
}
class Rapace extends Bird {
static { System.out.print("r1 "); }
public Rapace() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Eagle extends Rapace {
public static void main(String[] args) {
System.out.print("pre ");
new Eagle ();
System.out.println("eagle ");
}
}
**********************

The code compiles fine and gives the output :

r1 r4 pre b1 b2 r3 r2 eagle

I can't understand how !

Can someone explain what happens at execution time, what is first loaded ?.. to have this order in the output.

Many thanks by advance
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static blocks are executed first when you class is loaded into the JVM.
(not a necessary, but is ensured to be executed before any other code in the class).

Non-static class level blocks are executed before the constructor of the class is called, when an instance of the object is created.

The super class constructor is called (default no-arg constructor) before subclass constructor.

Now apply these facts, you will arrive at the output.
 
Abahmane mustafa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasanna Rajaperumal:
Static blocks are executed first when you class is loaded into the JVM.
(not a necessary, but is ensured to be executed before any other code in the class).

Non-static class level blocks are executed before the constructor of the class is called, when an instance of the object is created.

The super class constructor is called (default no-arg constructor) before subclass constructor.

Now apply these facts, you will arrive at the output.




ok Thanks a lot..

which class is first loaded ( Bird or Rapace)?

As Eagle extends Rapace , Rapace class is loaded before the Bird one.

Classes are loaded one after one following the inheritance tree. is that

true?

That is why we have r1 r2 before b1..

But how it come the Bird constructor is executed first?

the Bird class is loaded last so its constructor should excute after the

Rapace one , no ?

[ September 11, 2007: Message edited by: Abahmane mustafa ]
[ September 11, 2007: Message edited by: Abahmane mustafa ]
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I run it through the debugger and learned a few things myself.

First of all I was surprised to see that just by extending a class is good enough to get the class loaded. So execution starting point is actually going to start at the Rapace class. As we all known static blocks get to run first when the class is loaded. So you got r1 r4.

Now excution is going back to main and print pre.

Then new Eagle () constructor is called which will call Rapace() and which will call Bird();.

What prints out first in Bird() class is the { System.out.print("b1 "); }
I believe that is called the initialization block. Basically that line gets copied into all the constuctors in the same class. What I didn't know if that is copied to the first line or the last line. But I found it gets copied and end up in front of System.out.print("b2 "); So that's how you get b1 and b2 after the pre.

Now we are going back to Rapace class and same thing happen here, r3 gets printed out before r2.

then we finally jump back to main and print the last line Eagle.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As Eagle extends Rapace , Rapace class is loaded before the Bird one.

Classes are loaded one after one following the inheritance tree. is that

true?



The first line in any constructor whether implicit or explicit is the super() statement. This is why constuctors that are higher up the inheritance tree is executed first.

These rules you got to remember:

1) Static initializers are executed only once when the object is first loaded in the jvm. There could be many static initializers in any place in your code. Go from the up to the bottom.

2) Instance initializers are executed every time the instance is instantiated. Normal up-down order also applies. They could be found in any place within the class.

3) Instance initializers are executed before the constructors are executed.
 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic