I will start with my example: I have a class classload.Loadable
.
package classload;
public class Loadable {
static{
System.out.println("Loaded already.....");
}
public Loadable(){
System.out.println("Now created.....");
}
}
which will be loaded and created instance in the following 2 ways.
First:
public static void main(String[] args) throws Exception {
System.out.println("Starting .....");
Class.forName("classload.Loadable").newInstance();
}
Second:
public static void main(String[] args) throws Exception {
System.out.println("Starting .....");
classload.Loadable.class.newInstance();
}
Both gives same output as expected(since Class.forname
returns the same class
object):
Starting .....
Loaded already.....
Now created.....
I want to know which all scenarios we use Class.forname
and whereever we may use .class
object directly
Aucun commentaire:
Enregistrer un commentaire