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) {
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With