Re: Class.forName

Discussion in 'Java' started by Robert Klemme, Sep 13, 2004.

  1. "Ulf Jonson" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > Any one who can tell me the diffrerence beteween writing:
    >
    > Class.forName("com.mysql.jdbc.Driver");
    >
    > or
    >
    > Class.forName("com.mysql.jdbc.Driver").newInstance();
    >
    > is it the same thing?


    Not at all. The first line loads the class while the second line loads
    the class and creates a new instance of the driver. If you use the second
    approach, you can directly obtain a connection from the driver instance
    like:

    Driver d = Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection c = d.connect( dbUrl, properties );

    But these are equivalent:

    Class cl = Class.forName("com.mysql.jdbc.Driver");
    Class cl = com.mysql.jdbc.Driver.class;

    and

    Driver d = Class.forName("com.mysql.jdbc.Driver").newInstance();
    Driver d = new com.mysql.jdbc.Driver();

    > Is this the answer? Taken from sun website:
    > http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html


    FUP to comp.lang.java.programmer since this is not really database
    specific.

    robert
    Robert Klemme, Sep 13, 2004
    #1
    1. Advertising

  2. > But these are equivalent:
    >
    > Class cl = Class.forName("com.mysql.jdbc.Driver");
    > Class cl = com.mysql.jdbc.Driver.class;
    >
    > and
    >
    > Driver d = Class.forName("com.mysql.jdbc.Driver").newInstance();
    > Driver d = new com.mysql.jdbc.Driver();


    Not totally equivalent, since the first versions won't check the existency
    of the class at the compile time.
    Vincent Cantin, Sep 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    Re: Class.forname() vs clone()

    iksrazal, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,603
    iksrazal
    Jun 27, 2003
  2. Ahmed Moustafa

    performance of Class.forname()?

    Ahmed Moustafa, Jul 20, 2003, in forum: Java
    Replies:
    14
    Views:
    5,901
    Chris Smith
    Jul 29, 2003
  3. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,727
    H.MuthuKumaraRajan
    Nov 7, 2003
  4. cyril
    Replies:
    2
    Views:
    3,857
    cyril
    Aug 25, 2004
  5. Madni
    Replies:
    1
    Views:
    5,879
Loading...

Share This Page