Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Driver Program?

Tags:

java

driver

I'm really confused about Java classes and driver programs. What is the syntax of the code for a driver program and what should and should not be inside it? For the driver program we need perhaps to have a main program inside it but what about the Java class, don't we need to have any?

Do I need to have this for both the Java class and the driver program or only in the driver:

public static void main(String[] args) {

}
like image 398
Mah1990 Avatar asked Apr 13 '26 17:04

Mah1990


1 Answers

As referenced from here:

What is a "driver class"?

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."

Example:

This is not a driver class since it doesn't contain any main method. In this case, it has the method hello:

public class HelloWorld {
    public void hello() {
        System.out.println("Hello, world!");
    }
}

Versus this one - which is a driver class since it contains a main method, and is the class that runs HelloWorld:

public class HelloWorldDriver {
    public static void main(String[] args) {
        HelloWorld sayhello = new HelloWorld();
        sayhello.hello();
    }
}

Hence the name "driver class" - as the class HelloWorldDriver "drives" or rather, controls the instantiation and usage of the class HelloWorld.

like image 64
jrd1 Avatar answered Apr 15 '26 05:04

jrd1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!