When class loaded

Hi,

I like to know when classes are loaded into JVM. Different people gave me different answers to me like

1. while hitting import statement (import Class1)

2. while hitting declaration (Class1 obj)

3. while instantiating (new Class1())

Please clarify me.

Regards,

Govind

[321 byte] By [Govindarama] at [2007-11-27 6:24:49]
# 1

Compile the following.

package fora;

import fora.LoadOrderTest;

public class Test{

public static void main(String args[]){

System.out.println("Next LoadOrderTest lot");

LoadOrderTest lot;

System.out.println("Next new LoadOrderTest");

lot = new LoadOrderTest();

}

}

class LoadOrderTest{

}

Then delete LoadOrderTest.class

The run and see what happens.

cotton.ma at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 2
> I like to know when classes are loaded into JVM.There is no single answer. http://java.sun.com/docs/books/tutorial/ext/basics/load.html http://www.javalobby.org/java/forums/t18345.html~
yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 3
Hmmmm.
cotton.ma at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 4
> Hmmmm.?~
yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 5
It seems to me that a class is loaded for the first time when...- you access a static method or attribute of the class- you call the constructor- you explictly call Class.forNameIs that wrong?
cotton.ma at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 6

> It seems to me that a class is loaded for the first

> time when...

>

> - you access a static method or attribute of the

> class

> - you call the constructor

> - you explictly call Class.forName

>

> Is that wrong?

Hang on...

~

yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 7
I thought it'd be:a) when you reference a class for the first time (static access, constructor)b) when you explicitly load it with a classloaderwhere the b) is a special case really (the class is loaded when you load the class...well duh)
-Kayaman-a at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 8

> where the b) is a special case really (the class is

> loaded when you load the class...well duh)

That's what I had in mind.

import java.io.*;

import java.net.*;

class Foo {

public static void main(String[] args) throws Exception {

String className = "sun.tools.java.RuntimeConstants";

// Class clazz = Class.forName(className); <-- ClassNotFoundException

File f = new File("/dev/java/jdk1.6.0/lib/tools.jar");

if (!f.exists()) {

System.out.println("Pick a new file!");

System.exit(1);

}

ClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() }) {

protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {

System.out.println(">>> Loading " + name);

return super.loadClass(name, resolve);

}

};

Class c = cl.loadClass(className);

System.out.println(c); // interface sun.tools.java.RuntimeConstants

}

}

~

yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 9

> It seems to me that a class is loaded for the first

> time when...

>

> - you access a static method or attribute of the

> class

> - you call the constructor

> - you explictly call Class.forName

>

> Is that wrong?

My testing shows that it is how it works. But I do not know if there is a spec for that somewhere or how it might change from runtime to runtime.

package fora;

import fora.LoadOrderTest;

public class Test{

public static void main(String args[])throws Exception{

LoadOrderTest lot;// this will not blow up

System.out.println(LoadOrderTest.x);// this will blow up

Class.forName("fora.LoadOrderTest");.// this will blow up

lot = new LoadOrderTest();// this will blow up

}

}

class LoadOrderTest{

public static int x = 0;

}

cotton.ma at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 10

> My testing shows that it is how it works.

I'm not saying you're wrong. I am saying that there are other cases, however. You can control programatically when certain classes are loaded. See earlier response by Kayaman and myself. At the risk of sounding ridiculous (which I often do), classes are loaded when they are loaded. :o)

~

yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 11

> > My testing shows that it is how it works.

>

> I'm not saying you're wrong. I am saying that there

> are other cases, however. You can control

> programatically when certain classes are loaded. See

> earlier response by Kayaman and myself. At the risk

> of sounding ridiculous (which I often do), classes

> are loaded when they are loaded. :o)

>

> ~

Can I ask a follow up question though?

The orginial question asked among other things about this part

LoadOrderTest lot;

Which does not load the class in my test. Is that always true? Truth be told I find that a little odd (that it doesn't).

PS thank you for taking the time to work on that other example.

cotton.ma at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 12

> > My testing shows that it is how it works.

>

> I'm not saying you're wrong. I am saying that there

> are other cases, however. You can control

> programatically when certain classes are loaded. See

> earlier response by Kayaman and myself. At the risk

> of sounding ridiculous (which I often do), classes

> are loaded when they are loaded. :o)

>

> ~

in other words, classes are loaded when first required by a classloader that hadn't loaded them before.

What triggers that need is inconsequential.

jwentinga at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 13

> Can I ask a follow up question though?

>

> Which does not load the class in my test. Is that

> always true?

That I don't know. There may be some subtlety I'm unaware of. But in something like the following...

class Foo {

public static void main(String[] args) {

Bar b;

}

}

class Bar { }

...the variable "b" doesn't exist in the compiled bytecode. There's no reason to load the Bar class that I can see. The ClassLoader API states "Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime." I'd have to guess that has something to do with the behavior you see.

The following might be helpful (I've only skimmed it):

http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#72007

> PS thank you for taking the time to work on that other example.

You're certainly welcome. Thanks for posing the question! :o)

~

yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 14
> in other words, classes are loaded when first> required by a classloader that hadn't loaded them> before.> What triggers that need is inconsequential.Well stated. Thanks!~
yawmarka at 2007-7-12 17:44:01 > top of Java-index,Java Essentials,Java Programming...
# 15
> > http://java.sun.com/docs/books/jvms/second_edition/htm> l/ConstantPool.doc.html#72007> Yes i think it does answer my question.
cotton.ma at 2007-7-21 21:53:24 > top of Java-index,Java Essentials,Java Programming...
# 16

>

> in other words, classes are loaded when first

> required by a classloader that hadn't loaded them

> before.

> What triggers that need is inconsequential.

I can only come up with a contrived example where maybe it would be something.

B b;

boolean foundClass = false;

while(!foundClass){

/* some sort of class loader iteration

each loader is tried in turn to load the class

and then b = new B();

*/

}

If the class was loaded at the first part it wouldn't work.

But having said that, I concede the point that at least 99.9999% of the time it is inconsequential.

cotton.ma at 2007-7-21 21:53:24 > top of Java-index,Java Essentials,Java Programming...
# 17

>Which does not load the class in my test. Is that always true? Truth be told I find that a little

> odd (that it doesn't).

There's a formal spec for how JVMs behave. It requires that a JVM must behave as if the class is loaded when first "actively used" (unless you explicitly load it with a ClassLoader). Even a JVM that preloads classes is required to defer any error messages until that first active use.

A declaration, like the one you suggest, doesn't actually produce any code, merely allocates a slot in an object or stack frame, so it's not an "active use" which occurs when some element of the required class is activated.

malcolmmca at 2007-7-21 21:53:24 > top of Java-index,Java Essentials,Java Programming...