I’m fairly confused about the Class.forName in java. how do we explain what Class.forName is from a C# perspective?
use case: java.lang.Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Class.forName returns an instance of a Class object. Class is equivalent to the c# class object.
Class.forName forces classes to load, if they have not already been loaded. As part of the class loading process is invoking any static blocks defined in the class. A static block looks like,
Static blocks are invoked only once, the first time a class is loaded, so repeatedly calling Class.forName(“Foo”) will only cause “loaded Foo” to be printed once. Creating a new instance of Foo will also cause the class to be loaded, if it has not already been loaded.
Generally, JDBC drivers will register themselves by calling DriverManager.registerDriver() in a static block, which is why calling Class.forName() loads the driver.