The difference in functionality between the instance created by the class loader and the new keyword

I'm a little confused about the concept of loading and initializing classes

1: Class.forName("test.Employee").newInstance();
2: ClassLoader.getSystemClassLoader().loadClass("test.Employee").newInstance();
3: new test.Employee(); 

Each line of the code written above creates an instance of the class Employee, but I do not understand what the difference is in all three methods.

+4
source share
3 answers

The main differences between the three approaches come down to how classes are at runtime and what you can do with them.

For instance...

Class.forName("test.Employee").newInstance();

ClassLoader Employee test. , . , ...

ClassLoader.getSystemClassLoader().loadClass("test.Employee").newInstance();

"system" ClassLoader, , .

- , Class . , , .

, , , test.Employee , test.Employee , . interface .

ClassLoader , , , , , . , , interface s.

java.awt.Toolkit JDBC java.sql.Driver

ClassLoader , JVM. new ,

ClassLoader - , , ,

...

+2

, . , .

  • Class.forName("test.Employee")
  • ClassLoader.getSystemClassLoader().loadClass("test.Employee")
  • , Employee 1- .
+2

, :

public class ClassLoaderTest {
    public ClassLoaderTest() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        System.out.println("Current CL: "+getClass().getClassLoader());
        System.out.println("Parent CL: "+getClass().getClassLoader().getParent());

        // ClassTest class is defined in the current CL so I can dynamically create an instance
        // from the current CL and assign it (forName uses the current CL)
        ClassTest c1 = (ClassTest)Class.forName("ClassTest").newInstance();
        System.out.println("CL using forName: "+c1.getClass().getClassLoader());

        // the new keyword creates an instance using the current CL but doesn't have the
        // advantages of creating instances dynamically
        ClassTest c2 = (ClassTest) new ClassTest();
        System.out.println("CL using new: "+c2.getClass().getClassLoader());

        // Here we are indicating to use the system CL that in this case is the parent of the current CL
        Object c3 = ClassLoader.getSystemClassLoader().loadClass("ClassTest").newInstance();
        System.out.println("CL using system CL: "+c3.getClass().getClassLoader());

        // This won't work because the ClassTest is defined in the current CL but I'm trying to assign it to a
        // dynamically created instance of ClassTest associated to the system CL so:
        // java.lang.ClassCastException: ClassTest cannot be cast to ClassTest
        // ClassTest c4 = (ClassTest)ClassLoader.getSystemClassLoader().loadClass("ClassTest").newInstance();
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        CustomClassLoader cl = new CustomClassLoader(Test.class.getClassLoader());
        cl.loadClass("ClassLoaderTest").newInstance();
    }
}

:

Current CL: CustomClassLoader@1cfb549
Parent CL: sun.misc.Launcher$AppClassLoader@8ed465
CL using forName: CustomClassLoader@1cfb549
CL using new: CustomClassLoader@1cfb549
CL using system CL: sun.misc.Launcher$AppClassLoader@8ed465

ClassLoader (CL): www.javablogging.com/java-classloader-2-write-your-own-classloader/

0

Source: https://habr.com/ru/post/1537829/


All Articles