I'm not sure exactly what the JVM does. However, static blocks aren't excecuted until the class object is loaded (which is not necessary at load time). That indicates to me that the JVM doesn't fully recognize some classes until needed. That is not to say that the JVM doesn't use the classes in some way though. Which is why, for example, we need to do something like Class.forName("blah.blah"), when we load and use JDBC drivers. Is there something at issue here, or is this a mere curiosity?
Hi Nathaniel, What happens is that I have a JAR file (containing some classes) included in the classpath. One of these classes actually references another (user-defined) class in another package. Let's name this class X residing in package xyz When I attempt to execute a method of a class (e.g. A) in some package (abc), I encountered a "Class not found" exception. So I try to put that class into the JAR file and it works! So it leads me to think that although X is not being called, the classloader will attempt to load it and find all the other classes that it references and load them as well. And that's why it encounter the "class not found" exception, since the classloader cannot find it in the JAR file in the classpath. In addition, I believe this has to do with class-loading hierarchy, whereby there are a few classloaders for different classes at different locations (e.g. classpath, package etc)
I could imagine, that a class, which is in the jar-File, but not referenced by any other class, it will not be loaded. But if the class is referenced, it would be a lot of mini disk-accesses, if every class was loaded when needed. lib rt.jar is > 26 MB on my system, I don't think it is loaded completely every time.
Hi. When the classloader loads a class, it needs to load all classes referenced by this class, even if the flow inside the class will not lead to a real creation of any object. suppose:
the classloader must be able to find Y class, even if you always use 1 as a parameter here. If Y is not in your classpath - X will not load. However, loading of Y class itself is delayed. So - if Y references class Z which is not in your classpath - you will only find it later, when the JVM actually tries to instantiate Y. Nimo.
I was her plaything! And so was this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth