Using Multiple Files
We have already seen how to write several methods within one class. Now, we will learn how to create several classes with different methods. This section will not discuss how to create objects (instances of classes) yet – for now, we’re just dividing methods into different files as an organization trick.
Example: Separate class with static methods
Suppose we want to create a lot of methods that perform mathematical operations (average,
round, max, min, etc.). It would be nice to be able to reuse these methods in other projects, so
we will want to divide them into a separate class. (Actually, there is a Math class in the Java
libraries that contains these methods, but we will create our own.)
Creating a separate class with static methods is exactly like the classes we’ve written in the past
– the only difference is that the separate class will not have a main method. Here is our
MathOps class:
//stored in the file MathOps.java
public class MathOps
{
//assumes arr has at least one element
public static int max(int[] arr)
{
int m = arr[0];
for (int i = 1; i < arr.length; i++)
{
if (arr[i] > m) m = arr[i];
}
return m;
}
//assumes arr has at least one element
public static int min(int[] arr)
{
int m = arr[0];
for (int i = 1; i < arr.length; i++)
{
if (arr[i] < m) m = arr[i];
}
return m;
}
public static int round(double num)
{
if ((int)(num+0.5) > (int) num)
{
//num must have decimal of .5 or higher
//round up
return (int)(num+0.5);
}
else {
//round down
return (int)num;
}
}
public static double avg(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.length; i++)
{
sum += arr[i];
}
//need to cast to a double to avoid integer division
return sum / (double) arr.length;
}
}Calling static methods from a different class
Now, suppose we want to create a separate class with a main method that uses the MathOps
class. When we call static methods from another class, we will use the format:
ClassName.methodName(params);Here, ClassName is the name of the class that contains the method (in this case, MathOps),
and methodName is the name of the method we want to call. Here is a program that asks the
user for 10 numbers, and then prints the maximum, minimum, and average of those numbers:
//stored in Compute.java
import java.util.*;
public class Compute
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int[] nums = int[10];
for (int i = 0; i < 10; i++)
{
System.out.print("Enter a number: ");
nums[i] = Integer.parseInt(s.nextLine());
}
System.out.printf("Maximum: %d%n", MathOps.max(nums));
System.out.printf("Minimum: %d%n", MathOps.min(nums));
System.out.printf("Average: %d%n", MathOps.avg(nums));
}
}Compiling and running
Compiling works differently when we have multiple files, because we need to be sure to compile
all the source code files instead of just the one with the main method. Here’s how:
javac *.javaThis compiles ALL files with the .java extension – all your source code files. You could then
run the program with:
java Computewhich runs your program as usual. Note that you will run your program with the name of the file
that contains the main method.