Driver Class
The Driver class is much simpler. It has one feature, a class method public static void main(String[] args)
1. Everything that needs to be done will be done in the main method. A template might be:
public class Driver{
public static void main(String[] args){
}
}
A class feature belongs to the class, no object is necessary to use it. Examples include math methods we write
double five = Math.sqrt(25.);
to access the class function sqrt()
. We do not have to create a Math
object first:
Math mo = new Math(); // Incorrect
double five = mo.sqrt(25.); // Incorrect
Java uses the keyword static
as a function modifier to create class methods and attributes. Not all languages use the static
keyword in the same way. Be careful when using static
in discussing class-level features. In Java they are virtually synonymous, but in general they are not.
Accessing the Instance Class
Lets add an instance Ingredient
as i1
.
public static void main(String[] args){
Ingredient i1 = new Ingredient();
}
When the java compiler (javac
) sees an identifier it does not understand, it looks in
- the rest of the file being compiled
- the classes covered by the
import
statements - the directory the
.java
file is in -
- If it finds a
.java
file it also compiles it
- If it finds a
-
- If it finds a
.class
file it uses it
- If it finds a
So when our Driver.java
uses Ingredient
, the java compiler (re)compiles Ingredient.java
.
-
In Java, class methods have the modifier
static
↩︎