How Class.forName works exactly?
2011-08-21 10:58
218 查看
Question:
Answer:
转自:http://forums.devx.com/archive/index.php/t-156584.html
mcaashish 10-16-2006, 09:18 AM Hi, I have a doubt regarding the working of Class.forName() method. How exactly it works in jvm? What is the difference between Class.forName("oracle.jdbc.driver.OracleDriver") & DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())? If my JDBC code is something like this: Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName("Some other driver class"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:SID","user", "pwd"); Which class will be loaded and how will DriverManager class will know which driver to communicate? Can anybody help me on this? Thanks in advance.
Answer:
sudheerprem 12-25-2006, 02:44 AM When we create an instace of a class using new operator, it does two things 1. Load the class in to memory, if it is not loaded - which means creating in-memory representation of the class from the .class file so that an instance can be created out of it. This includes initializing static variables (resolving of that class) 2. create an instance of that class and store the reference to the variable. Class.forName does only the first thing. It loads the class in to memory and return that reference as an instance of Class. If we want to create an instance then, we can call newInstance method of that class. which will invoke the default constructor (no argument constructor). Note that if the default constructor is not accessible, then newInstance method will throw an IllegalAccessException. and if the class is an abstract class or interface or it does not have a default constructor, then it will throw an InstantiationException. If any exception araises during resolving of that class, it will throw an ExceptionInInitializerError. If the default constructor is not defined, then we have to invoke the defiend constructor using reflection API. But the main advantage with Class.forName is, it can accept the class name as a String argument. So we can pass the class name dynamically. But if we create an instance of a class using new operator, the class name can't be changed dynamically. Class.forName() inturn will call loadClass method of the caller ClassLoader (ClassLoder of the class from where Class.forName is invoked). By default, the Class.forName() resolve that class. which means, initialize all static variables inside that class. same can be changed using the overloaded method of Class.forName(String name,boolean initialize,ClassLoader loader) The main reason for loading jdbc driver using Class.forName() is, the driver can change dynamically. in the static block all Drivers will create an instance of itself and register that class with DriverManager using DriverManager.registerDriver() method. Since the Class.forName(String className) by default resolve the class, it will initialize the static initializer. So when we call Class.forName("com.sun.jdbc.odbc.JdbcOdbcDriver"), the Driver class will be loaded, instanciated and registers with DriverManager So if you are using new Operator you have to do the following things. Driver drv = new com.sun.jdbc.odbc.JdbcOdbcDriver(); DriverManager.registerDriver(drv);
转自:http://forums.devx.com/archive/index.php/t-156584.html
相关文章推荐
- how to get Class of primitive datatypes array . Class.forName(int[]) throws exception
- 反射中,Class.forName和classloader的区别
- 关于Class.forName(className).newInstance()的理解
- 关于Class.forName()
- Class.forName()知识
- [转]Class.forName()的作用与使用总结
- There is no getter for property named 'fieldName' in 'class java.lang.String'
- Class.forName()用法详解
- JDBC中注册驱动为什么使用Class.forName
- What does Class.forName(); do?
- 关于Java中Class.forName()的作用
- java中类名.class, class.forName(), getClass()区别
- Java反射:类名.class、class.forName()、对象.getClass区别 详解
- Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别
- Class.forName()和new和Instantiation()区别
- How to Set Classpath for Java on Windows and Linux
- Class.forName(xxx.xx.xx) 和newInstance区别
- class.forname().newInstance()
- Class.forName(driver)出错。(解决)
- Class.forName()用法 c26a 详解