24

I was reading through a Java textbook, and it mentions something called a "driver class". What is it, and how is it different from a normal class?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
lfaraone
  • 49,562
  • 17
  • 52
  • 70

4 Answers4

44

A "Driver class" is often just the class that contains a main. In a real project, you may often have numerous "Driver classes" for testing and whatnot, or you can build a main into any of your objects and select the runnable class through your IDE, or by simply specifying "java classname."

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • 6
    Can't say I've ever heard the class containing the main method being called a driver class before - it's usually something like "main class" or "entry point". That's just my experience though - the author may be using it in your sense... – Jon Skeet Apr 19 '09 at 17:29
  • 2
    I think it's a quirk of the Deitel+Deitel series. I've worked through a couple of their books, and I remember Driver to be the main class for a number of the examples. – Stefan Kendall Apr 19 '09 at 17:30
  • Ick - I guess we could do with knowing the book then :) – Jon Skeet Apr 19 '09 at 17:32
  • 1
    Well, I'm reading through a prep book for the AP Java test. Afaict, that's the terms they use on the test. – lfaraone Apr 19 '09 at 18:00
  • I've always thought of a driver class as a class that exists to hold a main method but doesn't really have any other purpose - it just "drives" all the other classes in the application to do their jobs. :? – David Z Apr 19 '09 at 18:13
  • 11
    In more than 10 years of java, I've never heard of the term 'driver class' associated with anything except the jdbc driver alone. If your book refers to the executable class with the "main" method as 'driver class', your book's author introduces an exotic term to the java community. – Florin Apr 19 '09 at 18:24
  • http://books.google.fr/books?id=BF6HyZSN7EsC&pg=PA733&lpg=PA733&dq=java+%22driver+class%22+main+-jdbc&source=bl&ots=YN2zBIP7Mi&sig=6Wf0hvFpCm70vqMhRI0KiM5BKBk&hl=fr&ei=1InrSbGIIKPNjAed5rWfCg&sa=X&oi=book_result&ct=result&resnum=9 does mention the term though... However, the JDBC reference is more likely. – VonC Apr 19 '09 at 20:36
  • 6
    Most of my college classes referred to the main class as the "driver." It drives the application. I've never heard that term used in the non-academic world though. – Andy White Apr 20 '09 at 03:27
13

Without context, it's hard to tell. Is it talking about a JDBC driver, perhaps? If so, the driver class is responsible for implementing the java.sql.Driver interface for a particular database, so that clients can write code in a db-agnostic way. The JDBC infrastructure works out which driver to use based on the connection string.

If the book wasn't talking about JDBC though, we'll need more context.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You might mention as an example the section "Creating a Hibernate Configuration File" (http://www.redhat.com/docs/en-US/JBoss_Developer_Studio/en/hibernatetools/html_single/index.html#hib_config_file), with the picture http://www.redhat.com/docs/en-US/JBoss_Developer_Studio/en/hibernatetools/html_single/images/plugins/plugins_1.png to further illustrate your answer. – VonC Apr 19 '09 at 20:41
  • I'm not sure - if the questioner is new to Java, introducing Hibernate into the mix, even for demonstration purposes, may not be terribly helpful. – Jon Skeet Apr 19 '09 at 22:04
11

According to my Java book:

A driver class is a class that is in charge of running other classes. Just as the computer term "Software Driver" refers to a piece of software that runs or drives something else.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
NellyRed
  • 111
  • 1
  • 2
3

"driver class" could refer to a procedural programming style involving: (1) "container classes" and (2) "driver classes"

Say that you are creating your own object as a container for data. Then you might want to create two types of classes: "containers" and "drivers"

The "container class" might contain: - instance variables to hold the relevant data - getters and setters - methods to support moving data in/out of class (parsing, translation) - limited computations

The "driver class" might contain: - main method that drives the execution of the overall task (aka entry point for execution) - calls to static methods, as with procedural programming - instances of container class objects to hold different data (may be organized in other data structures, e.g. arrays; manipulated to solve overall task)