Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JVM Class loading rules...need some info..

843811Jan 4 2003 — edited Jan 10 2003
Hi all,

I am trying to figure out when the JVM uses a ClassLoader to find/load a class. This may not be the proper forum so I will cross post this in a couple of places, I apologize now if it offends anyone.

Basically, what I need to figure out is when the JVM actually tries to find a class as it reads in a class bytecode. By this I mean, a class has imports, it has declarations, it has a constructor, methods, variables, etc. If I use the Class.forName().newInstance() to create a new class, from my understanding that new class has static initializers run, then the constructor. So let's take a simple example:

package mypackage;

import somepacakge.*;

public class MyClass
implements SomeInterface
extends AnotherClass
{
private SomeObjectType sot = null;

public MyClass()
{
sot = new SomeObjectType();
}

public void doSometing()
{
ThisClass tc = (ThisClass)sot;
}
}

Ok, so in the above example, we have an import, a private variable, a constructor that creates that variable, and another method (that we'll assume gets called at some point) that uses an entirely different class reference (ThisClass) and typecasts the SomeObjectType to it (for the sake of this example, let's assume this compiles and runs fine).

So the question here is, when this particular class if first created, when does the JVM actually pull this class instances ClassLoader to find the SomeObjectType .class bytecode (to load it so it can see it, use it to "rubber stamp" new objects), the ThisClass bytecode and so forth?

I am asking all this becuase I am trying to dynamically load classes and create classes from xml configured class names at runtime. The problem is, I have implemented by own classloader (extends URLClassLoader) and the loadClass() method. I actually purposely break the delegation model of the ClassLoader for my specific purposes. Instead of always checking parents first, I want to look in a list of "dependent" classloaders for classes. The main reason is, I want to enforce some classes to be outside the JVM classpath and only within the classpath of the classloader. Using the URLClassLoader delegation model, the parent classloader (all the way up to the system classloader) get first crack at finding the class(es).

So, does the JVM try to find ALL import xxx.* classes, then any variable class names, or does it ONLY try to find them when the class is actually used, such as in an assignment statement or as a parameter in a method?

I hope this makes some sense. I can't seem to find any information on how exactly the JVM uses classloaders to look for classes, in what order it tries to find classes a class references (imports, variables, instatiation, etc) and so forth.

Is there any documentation on this as well?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2003
Added on Jan 4 2003
16 comments
1,716 views