What's the difference between ClassLoader.load(name) and Class.forName(name) [closed]?

Consider this code class X { static{ System.out. Println("init class X..."); } int foo(){ return 1; } Y bar(){ return new Y(); } } The most basic API is ClassLoader. LoadClass(String name, boolean resolve) Class classX = classLoader.

LoadClass("X", resolve); If resolve is true, it will also try to load all classes referenced by X. In this case, Y will also be loaded. If resolve is false, Y will not be loaded at this point.

There doesn't seems to be any good reason for resolve=true. If nobody calls X.bar(), Y will never be needed, why should we load it at this point? And if Y is missing or corrupt, we'll get an error trying to load X, which is totally unnecessary.

Interestingly, this method is protected, so it's not easy to invoke it. Another method loadClass(name) simply calls loadClass(name,false). It's public, and it takes the sensible choice of resolve=false.So it is exactly what's needed by developers.

ClassLoader only loads classes, it does not initialize classes. We can inspect the class metadata, e.g. Its super class, its annotations, its methods and fields, etc. Without triggering the static initialization execution. This fact is very important for frameworks.

Now, Class. ForName Basically, Class. ForName(String name, boolean initialize, ClassLoader loader) calls loader.

LoadClass(name). And if initialize=true, the class is initialized - in the X example, we'll see "init class X..." printed. Class.

ForName(name) is the same as forName(name, true, currentLoader). Now, why would anyone want to initialize the class at this point? Wouldn't it be better if the class is initialized only when necessary?

A famous use case is JDBC initializing: Class. ForName("com.mysql.jdbc. Driver"); The convention is, a JDBC driver class registers itself in its static initializer.

The above code will trigger the static initialization, making the driver available for subsequent uses. From today's point of view, that design is really odd. We usually don't rely on static initializers.So there isn't much justification for initialize=true, and Class.

ForName(name) should be avoided. A "class literal" will return the class, without initializing it Class c = com.mysql.jdbc.Driver. Class; // actually compiled to Class c = Class.

ForName("com.mysql.jdbc. Driver", false, currentLoader); Now, what the heck is the "currentLoader"? It is the class loader of the current class class A { void foo() { currenLoader == THIS_A_CLASS.getClassLoader() } } When X.bar() is invoked for the first time, a "Y" class is needed.

What's happening is roughly class X bar() // new Y(); Class classY = currentLoader. LoadClass("Y"); Constructor cst = classY.getConstructor(); // next line will initialize Y (if not yet) // creating an instance of a class requires it be initialized Object y = cst.newInstance().

ClassLoader. LoadClass(String name) will attempt to load the class using the specified classloader. Class.

ForName(String name) will attempt to load the class using the default system classloader hierarchy.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions