File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes When is a class loaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When is a class loaded" Watch "When is a class loaded" New topic
Author

When is a class loaded

T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
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

Joined: Jan 28, 2008
Posts: 5575

Try This




Hope This Helps
T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
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

Joined: Jan 28, 2008
Posts: 5575

you are right
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Excellent explanation Joanne Neal
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
for further reading...

The Java Language Spec: Section 12.2 Loading of Classes and Interfaces


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When is a class loaded
 
Similar Threads
forward referencing
Initialization Block
When do classes get loaded into the JVM?
When is a class loaded?
ClassCircularityError