参考Class类的方法:
newInstance
public Object newInstance()//用类构造一个该类的对象
throws InstantiationException,
IllegalAccessException
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.
If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. forName//用类名返回一个Class对象
public static Class forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class named X to be initialized.
newInstance
public Object newInstance()//用类构造一个该类的对象
throws InstantiationException,
IllegalAccessException
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.
If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. forName//用类名返回一个Class对象
public static Class forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class named X to be initialized.
解决方案 »
- 看到一道题,说在什么情况下会出现问题,求大神帮忙
- 使用installanywhere打包遇到一个诡异问题,请大侠指点
- 为何无法引入自定义的类(附代码)?
- 如何添加 package包?
- BufferedInputStream的read方法的返回值
- 请教JDBC的分类,工作原理及其区别!
- 如何方便地进行61.1.1.1到061.001.001.001这样的格式转换?
- 聊天室问题:如何把客户端textfiled中的string发到服务器端 textarea中以及其他
- 大家好,google今天已经能用了,还是原来的网址www.google.com
- 做程序员还不如去开个店子卖点烟酒零食算了,郁闷,送分,送完为止(3)
- 我是初学者,问一下Java编译器的问题
- 简单的问题
Class class = Class.forName("com.xxx.xxx");
Object obj = class.newInstance();
就可以得到对象实例了。
Object obj = class.newInstance();当然在该例中,不要Object obj = class.newInstance();也是正确的,因为
你用Connection conn= DriverManager.getConnection(url,user,password); 连接数据源,它会根据你加载的驱动程序类也就是
在com.microsoft.jdbc.sqlserver.SQLServerDriver自动连接数据源的!