• 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

Order of class loading

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I want to know that what is the order of class loading when we inherit a class.
Say,class C extends B,class B extends A.
Now what is the order in which the classes are loaded.I checked the loading order by putting the static blocks in each of the classes which gave order A,B,C.But how come is this possible if i have constructors of the classes invoked in just reverse order?
Please guide me through...
Thanks...
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You try to load class C. But C depends on B. That means that class B needs to be loaded first. But B depends on A. That means that class A needs to be loaded first. After class A has been loaded, class B can be loaded. After class B has been loaded, class C can be loaded.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,
but doesn't the calling of constructors mean the loading of the classes,I mean if the constructor of class C is called first then perhaps class C should be considered loaded first.
Please guide through.
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhanshu Mishra wrote:Thanks Rob,
but doesn't the calling of constructors mean the loading of the classes,I mean if the constructor of class C is called first then perhaps class C should be considered loaded first.
Please guide through.


C can't be loaded first, because it depends on B.

Think of it like this; someone tells you to write a program in Java, but you haven't learned Java yet. Explicitly, the first thing that you should do is write a program. Implicitly, you'll have to learn Java first, because you can't create your program without it. Thus, you learn Java first, then you use it to write your program. You can apply the same logic to your question.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the first thing the constructor of class C will do is call the constructor of class B; either through an explicit call to super(...) or to an implicit call to super() if you don't add this call yourself. The constructor of class B in turn first calls the constructor of class A.
 
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Rob mentioned, the subclass constructor implicitly\explicitly calls the super(..) to initialize the superclasses. It is called as constructor chaining. You may find a reference of this http://download.oracle.com/javase/tutorial/java/IandI/super.html
reply
    Bookmark Topic Watch Topic
  • New Topic