Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

Question Regarding class loading precedence.

 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

* Now what i am confused is how the classes are loaded means in which order. it can be load
without invoking the constructor.


Question is from K&B.

Thanks & Regards
Jeetendra.......!!
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Bird{
static {System.out.print("Bird-Static ");} //Added an extra line for understanding
{System.out.print("b1 ");} // After object class control will return here and print b1. 3rd print.
public Bird(){ System.out.print("b2 "); }// as soon as control reach here it will initiate the object class and vice versa 4th print.
}

class Raptor extends Bird { // this class will loded and Static block will be executed just once and first.
static { System.out.print("r1 ");} // as soon as control reach here it will print r1 1st Print.
public Raptor() {System.out.print("r2 ");} // then this line again send the control to 3 // 6th print
{System.out.print("r3 ");} // 5th print instance block.
static { System.out.print("r4 "); } // 2nd static block of Bird 2nd print
}

class Hawk extends Raptor {
static {System.out.print("Hawk-Static ");} //Added an extra line for understanding
public static void main(String[] args) // As I know program start executing from here
{
System.out.print("pre "); // this line would be the next to be executed
new Hawk();// Now this line would send the control to 9 and Initiate the class Raptor
System.out.print("Hawk "); // and this will be the last print. Hawk
}
}

Output:
Bird-Static r1 r4 Hawk-Static pre b1 b2 r3 r2 Hawk

"Bird-Static" - this is the static block in the super most class (Hawk IS-A Raptor IS-A Bird).
"r1" - next super class (is Raptor) first static block
"r4" - next super class (is Raptor) second static block
"Hawk-Static" - actual class static block...

I hope it helps....
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialization consists of execution of any class variable initializers and static initializers of the class Test, in textual order. But before Test can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, and so on, recursively.

Source: http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.1.3
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am Really sorry but i did not get it. i want to know, means is it possible for any other classes without even loading the class consist of main.? Because as much as i know the the class which consist of main method invokes any other class through main method. your explanation is very good but may be i am little bit stupid.

You written a static block before creating instance of Hawk. oki that will be executed before the line which prints pre, when class Hawk will load.
Is it something like that a class is loaded when its constructor is invoked. means in this case if execution begins from new Hawk() then its ok that all of its super classes and their super class will be loaded and. and only then the class consisting main will be loaded and further executed...??

If we don't provide a constructor invocation then what..?? JVM automatically call the constructor in first line of main method.. Is it like that...?
Yes i read the JLS third edition. Even i make a printout for that chapter(i.e Chapter 12 Execution ) but still not getting it. I will be very thankful if some one will simplify it or summarize it for me. I am Sorry if the question is stupid..

Thanks & Regards
Jeetendra....!!
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no stupid question it is all process of learning..

You may be need to know how java class loader works:
Please refer:
http://www.javalobby.org/java/forums/t18345.html
http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html
http://www.java2s.com/Code/Java/Language-Basics/Examinationofthewaytheclassloaderworks.htm
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks For the link but it doesn't help very much. After the research on the links provided i find this one a little bit simple to understand and it is similar like what i was talking means

1st the public class SweetShop is loded.
2nd main method executes.
3rd after printing inside main. it invoked the default constructor provided by JVM of Candy class.
4th after execution of static block of Candy class and printing loading candy.
5th After printing "After Creating Candy" Searched and invoked the constructor of Class Gum by Class.forName(); method. Printed "Loading Gum".
6th printed "After Class Gum".
7th invoked the default constructor provided by JVM of class Cookie and printed "Loading Cookie".
8th Finally the last line of main get executed and printed "After Creating Cookie".

See i don't want to know that how class loader lodes the class having main method. i wanna know how the classes are loaded if they are in same package and related with IS-A or HAS-A relation. Means see the Question provided by me at 1st post of this thread how the class Hawk called other class without even executing its, 1st line means which prints pre..

Sorry for bugging you all this much but actually i don't have any constructor (Oops..!! i mean Instructor.) to navigate me in right direction.

Thanks & Regards
Jeetendra..!!
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like the initialization block in the first class, Bird, is NOT a static init block. Static init blocks
run when the class loads, whereas regular init blocks run about when the call to super() returns. Notice that that means
that if a class with a regular init block is never instantiated, that class's regular init block will not run (but static
init blocks run when the class is loaded, not instantiated, as stated above).

So the init block in the Bird class runs only when the Bird class's call to the Object constructor (super()) returns. And it's
the same case for the Raptor class which also has a regular init block as opposed to a static init block.

It looks like, from what you've said, that you are assuming that regular init blocks and static init blocks each run
when the class is loaded.

Ken Truitt
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems you guys not getting what i want to know. Let e try to explain little bit more..When A class get loaded if that is subclassed with any other class, After the 1st line where the subclass extends it or after the subclass invokes its constructor (Either provided by JVM or An overloaded Constructor.)



Thanks & Regards
Jeetendra...!!
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well run this code it may clear up some issues.

 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the first thing you have to know is that the term of "class loading" is differ than the term of "object instantiation"; that is why there are two blocks as static-blocks and regular-blocks.

Class loading have nothing to do with initialization, and hence it doesn't need to run a constructor. Assume a class which has a single STATIC variable named "x". Do I need to instantiate that class by it's constructor in order to access that variable? The answer is, no, you don't. Simply you can access the variable through the class name. Just like, MyClass.x (No need to create an object of MyClass). This can be done, because the MyClass class is already *loaded* to the memory. If it is not already loaded, the invocation of the expression MyClass.x will cause the class to be "loaded".

This "loading" process will be done by the JVM, and especially java.lang.ClassLoader is mainly responsible for that loading process. Please note that this "loading" will not create an object of the class (no constructor access). It just loads the STATIC members of the class (static variables, static methods, static inner classes...) to the memory. In addition to that, more importantly, it also loads all of the static members in the super class, and all of the static members in implemented interfaces (if any) of MyClass.

When considering the expression MyClass.x (where x is a static variable), it requires that class to be loaded in to the memory before you can access it. Therefore if the class is not already loaded, it will give an order to the JVM to load this class. While this loading, it loads all of the static members of this class (static members only), as I described above. That is why that the expression MyClass.x is legal.

For more information, this class "loading" process will be done with the FIRST ACCESS of the class. Therefore, even though you didn't access a static member of the MyClass class, just the instantiation of the MyClass class (i.e. new MyClass();) will cause the MyClass to be loaded.

Now let's have a look at the Hawk class. This class has a main method, and as all of you know, that method will be called by the JVM, when you 'run' the program. Main method is a static method. So, BEFORE the main method can be called, it is required to "load" the Hawk class. As I described above, it also loads all of it's super classes, and all implemented interfaces, if any. And thus due to the class hierarchy Bird -> Raptor -> Hawk classes (it's static members) will be loaded, BEFORE the main method is called. (Again, this ‘loading’ process has nothing to do with object instantiation).

Since a regular initialization block will be called with the object instantiation process, the static block, what do you call "static initialization block" will be called with this class loading process. Therefore all of the static blocks in Bird class, Raptor class and then Hawk class will be called, BEFORE calling the main method.

HTH

Devaka
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!! Nice and detailed explanation Devaka!!
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superb...!! Thank you very very much Devaka. Now i got it its exactly what i am seeking for. Really Thanks Very Much for your help.

Thanks & Regards
Jeetendra..!!
 
Devaka Cooray
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jeetendra Choudhary wrote:Yes Its seems like all the classes are loaded before executing the main method. but what about the line which prints pre.?is it like main method is executed only after its container classes constructor invoked...?



Which makes you to think like that? For the first program, the output is "r1 r4 pre b1 b2 r3 r2 Hawk", where r1 and r4 are resulted from static initialization blocks.
reply
    Bookmark Topic Watch Topic
  • New Topic