newInstance() and new

阅读更多

相同点

newInstance()  and new can both create new object.

 

不同点

newInstance() must follow by forName(className), and the class is loaded

 弱类型。低效率。只能调用无参构造。

 

class with new will loaded only when called

强类型。相对高效。能调用任何public构造

 

 

newInstance simple use

String className = "Example";
class c = Class.forName(className);
factory = (ExampleInterface)c.newInstance();

 

JDBC case

Class.forName(driverClassName);

someones followed the newInstance(), actually no need.

 

// Driver will register to DriverManager in static block
			 public class MyJDBCDriver implements Driver {
			 static {
			 DriverManager.registerDriver(new MyJDBCDriver());
			 }
 

Q:newInstance exception handle?

你可能感兴趣的:(new,newInstace,jdbc)