In Java, you create an instance of a class by its class name, which must be the default constructor and cannot write its own method of construction.
Method 1:
Userdao Userdao=null; Properties Prop=new properties (); InputStream Instream=daofactory.class.getclassloader (). getResourceAsStream ("daoconfig.properties"); Prop.load (instream); String userdaoclass=prop.getproperty ("Userdaoclass"); Class Clazz=class.forname (Userdaoclass); userdao= (Userdao) clazz.newinstance ();
Method 2:
Userdao Userdao=null; Properties Prop=new properties (); InputStream Instream=daofactory.class.getclassloader (). getResourceAsStream ("daoconfig.properties"); Prop.load (instream); String userdaoclass=prop.getproperty ("Userdaoclass");//Gets the class name userdao= (Userdao) class.forname (Userdaoclass) from the configuration file. Newinstance ();
Creating an instance of a class from a class name in Java