Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When is a class loaded

 
T George
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering when is a class loaded

I was reading about static init block and they are loaded when the class is loaded
if there are multiple classes in the source file and there are multiple static init blocks in each of them then what will be the sequence

will it be in the order they are declared in the file?


does inheritance have any effect on this(i doubt it though)
I mean if there are 3 classes A, B and C declared in the same order
B inherits A and C has the main function does it make a difference?

Just a small confusion
Thank you in advance
Thomas
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try This




Hope This Helps
 
T George
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ouch thats a kick in the head

so it is the other way around....

declaration of classes in the file does not affect it but the inheritance does

So I guess that means all the superclasses of the class that is first loaded are loaded and nothing else even if they are in the same file

And then it goes in order of inheritance, top to bottom, again irrespective of how they are declared
Please correct me if I am wrong

Thanks a lot Seetharaman
Thomas
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by T George:
So I guess that means all the superclasses of the class that is first loaded are loaded and nothing else even if they are in the same file


You need to distinguish between source files and byte code (.class) files. You can put as many classes as you want in a source file (as long as only one top level class is declared public), but when you compile it you will get a separate .class file for each class. So when you load a class, there are no other classes in that file to load and hence no order to maintain.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation Joanne Neal
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for further reading...

The Java Language Spec: Section 12.2 Loading of Classes and Interfaces
 
T George
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:


So when you load a class, there are no other classes in that file to load and hence no order to maintain.


Thanks for the help Joanne
I did think about this but i thought there might be some info about the source file(like names of other classes or something) stored in the class file(byte code) which I don't know about(fairly new to java )
Maybe I should check what goes into a class file in JLS(i think) however it is clear that it does not matter whats in the source file regarding which class is loaded

@Bill
Thanks for the link
JLS seems a lil out of my league but I will try to use it once I am comfortable with the language

Thank you again for the help
much appreciated
Thomas
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write yourself a tiny class, compile it to bytecode with javac then analyse it with the javap tool.

javac MyTinyClass.java
javap -c MyTinyClass

You get a printout of what the individual bits of bytecode mean; they look quite like assembler. You will find that other classes quoted in your original .java file reappear in that printout, so their name is retained in the bytecode. As far as I know, the class is loaded the first time it is mentioned in the code.

You would load the class with the main method first, then java.lang.String because the main method has String[] in its parameters. Then whichever classes are mentioned in main.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic