How does Class.forName() work?

Class.forName simply loads a class, including running its static initializers, like this:

class Foo {
    static {
        System.out.println("Foo initializing");

public class Test {
    public static void main(String [] args) throws Exception {

All the rest of the procedure you’re talking about is JDBC-specific. The driver – which implements Driver, it doesn’t extend DriverManager – simply registers an appropriate instance using DriverManager.registerDriver. Then when DriverManager needs to find a driver for a particular connection string, it calls connect on each registered driver in turn until one succeeds and returns a non-null connection.

Note that this way of registering drivers is reasonably old-fashioned – look at the docs for DriverManager for more modern ways of getting at a data source.

Leave a Comment