• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Class.forName() in Java

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain to me what the below line of code is doing. I understand that by calling Class.forName(we are initializing everything that is static in that class). I may be a little off saying that but I still don't get the general's of it. Also the exact example below.

Details are great! Thanks to anyone that is willing to give some detailed input on this.

Class.forName("oracle.jdbc.driver.OracleDriver");
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It dynamically loads the class oracle.jdbc.driver.OracleDriver, which is a JDBC driver for the Oracle database.

Java's database access API, JDBC, is set up in such a way that you (mostly) don't have to worry what exact database you are using when you write your program. Vendors of different databases provide drivers to do the database-specific things that are necessary when you want to use a database. It used to be the case that you needed to initialize the driver with a Class.forName(...), so that JDBC knew that this driver was available.

However, since JDBC version 4.0 it is no longer necessary to do this explicitly - JDBC can now discover drivers by itself.

So, if you are not using a really old version of Java or a really old database driver, you can just leave this line of code out - it is not necessary.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response. That cleared my example up for me.
Generally though, would I want to use Class.forName() in any other case?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there are other cases where you might want to load a class whose name you know. In those cases, usually the class is going to implement an interface which your code already knows about. This kind of architecture is often called "plugin" as you're taking a class which you have written and plugging it in to some code which just wants an implementation of some interface.
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based off of your response, I put an example together. In this scenario, how would the Class.forName() be used. (if it would be at all in this case). I'm just trying to understand this and the exact case it would be used. You did great explaining it but I'm still lost in terms of the benefit and when I'd use it. Thank you.

With a base example, give or take what is actually listed in my sample below, when would I use Class.forName()?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too complicated a question for “Beginning”: moving discussion.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic