这两个声明有什么区别? [重复]

时间:2020-12-08 13:27:19

Possible Duplicate:
What is difference between “Class.forName()” and “Class.forName().newInstance()”?

可能重复:“Class.forName()”和“Class.forName()。newInstance()”之间有什么区别?

In my android apps to connect mysql server using jdbc connection. In that I want to know difference

在我的Android应用程序中使用jdbc连接连接mysql服务器。在那我想知道差异

`Class.forName("com.mysql.jdbc.Driver");` 

and

Class.forName("com.mysql.jdbc.Driver").newInstance(); 

and also how to set the timeout functions, when server is not connected?

以及如何在未连接服务器时设置超时功能?

1 个解决方案

#1


3  

Class.forName() returns a Class object -- an object that represents a particular Java class.

Class.forName()返回一个Class对象 - 一个表示特定Java类的对象。

newInstance() -- a method of java.lang.Class -- will use the no-argument constructor of the represented class to create an instance of that class.

newInstance() - java.lang.Class的方法 - 将使用所表示的类的无参数构造函数来创建该类的实例。

For your question: (Reference from http://www.coderanch.com/t/385654/java/java/Difference-between-Class-forName-Class)

对于您的问题:(参考http://www.coderanch.com/t/385654/java/java/Difference-between-Class-forName-Class)

Class.forName() gets a reference to a Class, Class.forName().newInstance() tries to use the no-arg constructor for the Class to return a new instance. No surprises so far. Another common use for Class.forName() is to cause the Class to be loaded, since some types of Classes have side-effects from the loading process which is required for other purposes. JDBC is a large user of this process, since the Driver class is required to register itself with the DriverManager Class when it gets loaded.

Class.forName()获取对Class的引用,Class.forName()。newInstance()尝试使用Class的no-arg构造函数返回一个新实例。到目前为止没有惊喜。 Class.forName()的另一个常见用途是使类加载,因为某些类型的类具有来自加载过程的副作用,这是其他目的所必需的。 JDBC是此过程的大用户,因为Driver类在加载时需要使用DriverManager类注册自身。

In the deep dark days of Java, probably v1.1.8 but possibly up to Java 1.2, there was an issue that the default ClassLoader would not load a Class until it had an instance created. In these cases, JDBC code would fail if you used Class.forName() rather than Class.forName().newInstance().

在Java的黑暗时期,可能是v1.1.8但可能达到Java 1.2,存在一个问题,即默认的ClassLoader在创建实例之前不会加载Class。在这些情况下,如果使用Class.forName()而不是Class.forName()。newInstance(),则JDBC代码将失败。

While newInstance() would create an instance that got immediately thrown away, it was required to make Class.forName() work correctly. This work around is no longer required.

虽然newInstance()会创建一个立即丢弃的实例,但是需要使Class.forName()正常工作。不再需要这种解决方法。

#1


3  

Class.forName() returns a Class object -- an object that represents a particular Java class.

Class.forName()返回一个Class对象 - 一个表示特定Java类的对象。

newInstance() -- a method of java.lang.Class -- will use the no-argument constructor of the represented class to create an instance of that class.

newInstance() - java.lang.Class的方法 - 将使用所表示的类的无参数构造函数来创建该类的实例。

For your question: (Reference from http://www.coderanch.com/t/385654/java/java/Difference-between-Class-forName-Class)

对于您的问题:(参考http://www.coderanch.com/t/385654/java/java/Difference-between-Class-forName-Class)

Class.forName() gets a reference to a Class, Class.forName().newInstance() tries to use the no-arg constructor for the Class to return a new instance. No surprises so far. Another common use for Class.forName() is to cause the Class to be loaded, since some types of Classes have side-effects from the loading process which is required for other purposes. JDBC is a large user of this process, since the Driver class is required to register itself with the DriverManager Class when it gets loaded.

Class.forName()获取对Class的引用,Class.forName()。newInstance()尝试使用Class的no-arg构造函数返回一个新实例。到目前为止没有惊喜。 Class.forName()的另一个常见用途是使类加载,因为某些类型的类具有来自加载过程的副作用,这是其他目的所必需的。 JDBC是此过程的大用户,因为Driver类在加载时需要使用DriverManager类注册自身。

In the deep dark days of Java, probably v1.1.8 but possibly up to Java 1.2, there was an issue that the default ClassLoader would not load a Class until it had an instance created. In these cases, JDBC code would fail if you used Class.forName() rather than Class.forName().newInstance().

在Java的黑暗时期,可能是v1.1.8但可能达到Java 1.2,存在一个问题,即默认的ClassLoader在创建实例之前不会加载Class。在这些情况下,如果使用Class.forName()而不是Class.forName()。newInstance(),则JDBC代码将失败。

While newInstance() would create an instance that got immediately thrown away, it was required to make Class.forName() work correctly. This work around is no longer required.

虽然newInstance()会创建一个立即丢弃的实例,但是需要使Class.forName()正常工作。不再需要这种解决方法。