What does Class.forname method do?
A call to Class.forName("X") causes the class named X to be dynamically loaded (at runtime). A call to forName("X") causes the class named X to be initialized (i.e., JVM executes all its static block after class loading). Class.forName("X") returns the Class object associated with the "X" class. The returned Class object is not an instance of the "x" class itself.
Class.forName("X") loads the class if it not already loaded. The JVM keeps track of all the classes that have been previously loaded. This method uses the classloader of the class that invokes it. The "X" is the fully qualified name of the desired class.
For example,
package com.xyzws; class AClass { static { System.out.println("static block in AClass"); } } public class Program { public static void main(String[] args) { try { Class c = Class.forName("com.xyzws.AClass"); } catch (ClassNotFoundException e) { } } }
The output is
static block in AClass
Here is one example that uses returned Class to create an instance of AClass:
package com.xyzws; class AClass { public AClass() { System.out.println("AClass's Constructor"); } static { System.out.println("static block in AClass"); } } public class Program { public static void main(String[] args) { try { System.out.println("The first time calls forName:"); Class c = Class.forName("com.xyzws.AClass"); AClass a = (AClass)c.newInstance(); System.out.println("The second time calls forName:"); Class c1 = Class.forName("com.xyzws.AClass"); } catch (ClassNotFoundException e) { ... } catch (InstantiationException e) { ... } catch (IllegalAccessException e) { ... } } }
The output is
The first time calls forName: static block in AClass AClass's Constructor The second time calls forName: //Calss has been loaded so there is not "static block in AClass" printing out
JDBC Driver Is a Good Example
You may have experience working with JDBC Drivers. For example, the classloader attempts to load and link the
Driver
class in the "org.gjt.mm.mysql" package. If successful, the static initializer is called.Class.forName("org.gjt.mm.mysql.Driver"); Connection con = DriverManager.getConnection(url,?myLogin", "myPassword");
Let's see why you need Class.forName() to load a driver into memory. All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.
The MySQL JDBC Driver has a static initializer looks like this:
static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } }
JVM executes the static block and the Driver registers itself with the DriverManager.
You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.
2 comments:
These double standards are outrageous!. Air Jordan 1 Yellow And Black 2017, Won use the term confident because in my mind that puts it too far up the scale but I am optimistic. I remain optimistic, and part of my job is to optimistic, but there a basis for optimism. We had a GM conference call Monday and most of our clubs remain optimistic.
Air Jordan 13 Retro Blue And Black, That's an indictment on their people, their DNA and their character. We always say if a program is talking negatively about us, they've probably run out of positive things to say about themselves."Listen to the full interview:John Canzano BFT Podcast . BFT Interview: Mario CristobalRegistration on or use of this site constitutes acceptance of our User Agreement, Privacy Policy and Cookie Statement, and Your California Privacy Rights (each updated 1/1/20).
Reason why we can stop touching our faces. {tag: Yeezy 350 Cream White Ebay}Prisons should release low level, non public safety offenders who are near their release term anyway. {tag: Yeezy 750 All White}
Jordan 1 Womens Black And White, The force is strong in this one.It been 32 years since "Return of the Jedi," more than three decades of patient waiting, crudely re jiggered re releases and some hugely divisive prequels.But here comes "The Force Awakens," and let the fans rejoice.The seventh "Star Wars" film, and the first to have no controlling supervision from creator George Lucas, it a careful recreation of what really drove his first three epics of duels and droids.And while bringing back the original cast some for starring roles, some for quicker appearances it also introduces us to three new heroes, and sets us up for new adventures ahead.The story mostly by directorJ. J. Abrams and franchise veteran Lawrence Kasdan frankly copies the set up of the first film.
Aston Martin Lagonda leader and additionally band top dog, Doctor Andy Palmer believed: "Certainly nada reflects Aston (New Yeezys 2020) Martin's (New Jordan Releases 2020) persistence (Coach Outlet Online) for (Michael Kors Outlet Store) giving you unique, Memorable variations to it has target (Cheap Yeezy Shoes Sale) market a lot greater than the V12 Speedster. The 88 (Ray Ban Outlet Store) palm started motor motorcycles is without a (Ray Ban New Wayfarer Polarized) doubt internationally recognized by way of pushing hobbyists and fans equal worldwide. So so i am enthralled not just through what features motorists, Having said that ture over just what (Coach Outlet Store Online) results from.
Our group attribute this task cheaply, Sparkly wrought
Post a Comment