Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Java ClassLoader load inner classes?

If I have a inner class declaration such as:

Class A {     public static class B {     } } 

followed by:

Class<?> implClass = getClass().getClassLoader().loadClass("A"); 

Will the A$B inner class be loaded as well? What if the B inner class was not declared as "static" ?

like image 625
Janik Zikovsky Avatar asked Jul 02 '14 18:07

Janik Zikovsky


People also ask

What does a ClassLoader do in Java?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

How does JVM ClassLoader work?

The ClassLoader works based on a set of operations given by the delegation model. They are: ClassLoader always follows the Delegation Hierarchy Principle. Whenever JVM comes across a class, it checks whether that class is already loaded or not.

Is it possible to load a class by two ClassLoader?

A class is always identified using its fully qualified name (package. classname). So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.

Does Java support inner classes?

Java Inner Classes In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.


1 Answers

Once the code is compiled, there's no such thing as an inner class. If you look at the results of javac, you'll see two files:

A.class A$B.class 

So class B is not loaded when A is loaded, B just happens to be defined in A.


Edit

For example, given these two files,

package kuporific;  public class A {     private static class B {}     private class C {} } 

and a build.gradle file (for convenience):

apply plugin: 'java' 

First, build by running gradle build. Then, unzip the resulting JAR file (located in build/libs):

├── META-INF │   └── MANIFEST.MF └── kuporific     ├── A$B.class     ├── A$C.class     └── A.class 

Opening each file (in IntelliJ, for example), reveals what the compiler has done:

  • A.class:

    package kuporific;  public class A {     public A() {     }      private class C {         public C() {         }     }      private static class B {         public B() {         }     } } 
  • A$B.class:

    package kuporific;  class A$B {     private A$B() {     } } 
  • A$C.class:

    package kuporific;  import kuporific.A;  class A$C {     private A$C(A this$0) {         this.this$0 = this$0;     } } 

Notice that

  1. A$B does not have a reference to its parent, A, while A$C does. This is because the former is a static inner class, and the latter is not, and
  2. both A$B and A$C are now package private classes.

This is how non-static inner classes are able to directly reference their parent instance's fields and methods, and vice versa. (Any private fields of the parent class referenced in an inner class are made package private as well.)

Next, let's see what effect loading class A has on A$B and A$C.

First, add the following Java class:

package kuporific;  public class Main {     public static void main(String[] args) throws ClassNotFoundException {         Main.class.getClassLoader().loadClass("kuporific.A");     } } 

Now add the following to the build.gradle file:

apply plugin: 'application' mainClassName = 'kuporific.Main' applicationDefaultJvmArgs = ["-verbose:class"] 

The -verbose:class outputs all classes that are loaded by the JVM (see Java - Get a list of all Classes loaded in the JVM).

Run gradle run on the command line (which runs the main method of Main); the output (with my added notes) is

:compileJava :processResources UP-TO-DATE :classes :run [Opened /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar] [Loaded java.lang.Object from /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar] # Lots of omitted output... [Loaded kuporific.Main from file:/tmp/build/classes/main/]         ^ here! [Loaded sun.launcher.LauncherHelper$FXHelper from /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar] [Loaded java.lang.Class$MethodArray from /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar] [Loaded kuporific.A from file:/tmp/build/classes/main/]         ^ here! [Loaded java.lang.Shutdown from /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar] [Loaded java.lang.Shutdown$Lock from /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/jre/lib/rt.jar]  BUILD SUCCESSFUL  Total time: 6.502 secs 

We can see when kuporific.Main and kuporific.A were loaded, and we do not see either kuporific.A$B or kuporific.A$C being loaded.

like image 144
kuporific Avatar answered Sep 20 '22 23:09

kuporific