21

Assume I have the following class:

class Caller {
  public void createSomething() {
    new Something();
  }
}

Would executing this line:

static void main() {
   Class<?> clazz = Caller.class;
}

cause the JVM to load the class Something or is the class loading deferred until the method createSomething() is called?

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • 8
    `import` is just syntactic shorthand, so you can reference `File` later rather than `java.io.File` every time. So, as an aside, this is nothing to do with classloading, directly. An import by itself does nothing at runtime; it's just there for you and the compiler. – Sean Owen Sep 26 '11 at 20:12
  • 3
    Java loads classes when they're referenced. "Unneeded" classes will not be loaded, either at compile-time or at runtime. – parsifal Sep 26 '11 at 20:19

3 Answers3

28

A class is loaded only when you require information about that class.

public class SomethingCaller {
    public static Something something = null; // (1) does not cause class loading
    public static Class<?> somethingClass = Something.class; // (2) causes class loading

    public void doSomething() {
        new Something(); // (3) causes class loading
    }
}

The lines (2) & (3) would cause the class to be loaded. The Something.class object contains information (line (2)) which could only come from the class definition, so you need to load the class. The call to the constructor (3) obviously requires the class definition. Similarly for any other method on the class.

However, line (1) doesn't cause the class to be loaded, because you don't actually need any information, it's just a reference to an object.

EDIT: In your changed question, you ask whether referring to Something.class loads the class. Yes it does. It does not load the class until main() is executed though. Using the following code:

public class SomethingTest {
    public static void main(String[] args) {
        new SomethingCaller();
    }
}

public class SomethingCaller {
    public void doSomething() {
        Class<?> somethingClass = Something.class;
    }
}

public class Something {}

This code does not cause the Something.class to be loaded. However, if I call doSomething(), the class is loaded. To test this, create the above classes, compile them and delete the Something.class file. The above code does not crash with a ClassNotFoundException.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • 1
    But would referencing SomethingCaller.class cause the class Something to be loaded (ignoring the static fields). (Please see updated question for details) – Garrett Hall Sep 26 '11 at 23:05
  • Strangely, I thought the same, and then I tested this -- just referring to Something.class doesn't seem to load it! Instantiating it or something does of course. – Sean Owen Sep 27 '11 at 07:35
  • Just to clarify, the class is not loaded until the line that contains the Something.class is executed. – Matthew Farwell Sep 27 '11 at 07:44
  • Okay thanks for the detailed responses, that is what I suspected. – Garrett Hall Sep 27 '11 at 09:55
  • 6
    I don't think this is correct anymore for (2) - Java 8. I can assign `Class> k = Foo.class;` and get a Class instance that has the type name but it hasn't executed any static blocks in the class. – davidbak Oct 07 '15 at 17:03
  • @davidbak Your line of code will cause the class to be loaded but not to be initialized, as this line of code does not fall into the rules of when static initialization will happen. – arxakoulini Mar 16 '18 at 16:24
  • 1
    @NikiforosArchakis - thanks for the clarification! I'll now want to read about the "lifecycle of a Java class" ... – davidbak Mar 16 '18 at 16:37
2

Yes, that will cause the class to load when the class containing the File.class reference is loaded. The only way to not do this is to reference a class by reflection. Then you can control when it's loaded.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • So when the program executes a line containing File.class then all the class referenced inside the File class are also loaded? – Garrett Hall Sep 26 '11 at 20:43
  • Updated to say: it's not loaded until used, so in general, you should just not worry about what you're worrying about in this question and leave it to Java. See comment on other question for an interesting detail. – Sean Owen Sep 27 '11 at 07:35
-1

If you have performance requirements this strict, you should consider writing a custom ClassLoader object. This would let you can dump classes from memory when they aren't needed any more.

You'll want to check out the loadClass, findClass and probably the defineClass methods in the ClassLoader class, overriding load and find and using defineClass in the implementation. A google search on writing custom class loaders will reveal lots of sample code to do this, but basically you are going to cache the class results in a Map (class name to Class), then provide some callback mechanism to remove loaded classes from your cache when they aren't needed.

good luck.

Eric Lindauer
  • 1,813
  • 13
  • 19