The Java class unit is used in Java for several different purposes:
The class has the main method for an application program
The class is a container for a collection of utility methods
The class defines a type that represents concrete or abstract objects in some application/problem context.
The class defines a type that doesn't correspond directly to the kind of objects in an application/problem.
However, such a class may be useful in applications - for example, to store application objects or to help process application objects.
The class must contain a main method
The main method is static
The class may contain other static methods that can be called by main.
The class may contain static data members. These are shared by all methods in the class.
The main method (and other methods in the class) may create and use local variables of basic and/or class types.
An application class typically does not define a new type.
An application class must have a main method in order to be executable.
/** * Application class that prompts for an input string, * converts the string to upper case and prints the * converted string. * @author glancast */ public class StringApp { /** * The main program of this application * @param args - command line arguments (not used) */ public static void main(String[] args) { Scanner in = new Scanner(System.in); String line; greeting(); System.out.print("Enter an input line: "); line = in.nextLine(); line = line.toUpperCase(); System.out.printf("\nHere is your input converted to upper case:\n"); System.out.println(line); } /** * Prints an introductory message to explain * what input this application expects and what it does. */ public static void greeting() { String msg = "This program will read an input line, convert it\n" + "to upper case, and print the converted string."; System.out.println(msg); } }
Contains a collection of utility methods which are all static.
The static member methods typically operate only on data passed as parameters, so there are typically no data members in the class.
The class typically contains no data members. But if it does, these are static data members.
Static methods can only access static data members of a class (provided there are any such members).
A utility class with static methods (with or without static data members) does not define a new type.
public class Stats { /** * Returns the maximum of an int array. * @param a */ public static int maximum(int[] a) { int max = Integer.MIN_VALUE; for(int i = 0; i < a.length; i++) { if ( a[i] > max ) { max = a[i]; } } return max; } /** * Returns the mean (average) of values in an integer array * @param a - the array * @return the mean value of the array members */ public static double mean(int[] a) { double sum = 0.0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum / a.length; } }
A type defines both data and operations on that data.
A Java class can be used to define a new type.
Variables can then be defined of that class type.
Each variable can have its own instance value of the class type.
The class has data members that are not static. These are called instance members.
Variables can be declared of this class type.
Values can be created and assigned to such variables. Each variable will have its own separate set of instance data members.
So unlike static data members of a class, instance members are not shared.
The methods of a class that defines a type are not static. These are called instance methods.
An instance method operates on the instance data of the instance used to call it.
Instance data members of the class are accessible to each of the instance methods (but not to any static methods).
Typically such a class is not used as an application and so doesn't need a main method.
Instead, some application class will use a class like this. For example, an application class may declare, create and use an instance of this class type.
A StopWatch could be an concrete object in some application/problem context.
public class StopWatch { /* private instance members */ private double start; /* public instance methods */ /** * Initialize this stopwatch start time to present time */ public StopWatch() { start = System.currentTimeMillis(); } /** * Reset this stopwatch to the present time */ public void reset() { start = System.currentTimeMillis(); } /** * Returns the elapsed time in milliseconds since this * stopwatch was created or since it was reset. */ public double elapsedTime() { double now = System.currentTimeMillis(); return (now - start); } }
The Counter class may not correspond to any object directly specified in a problem or application context, but this class may be used in writing and application in such a context.
public class Counter { /* private instance members */ private String id; private int value; /* public instance constructor and methods */ /** * Initialize this counter's id name = idName and value = 0. * @param idName */ public Counter(String idName) { id = idName; value = 0; } /** * Increment this counter's value by 1 */ public void increment() { value++; } /** * Return the value of this counter. */ public int val() { return value; } /** * (Inherited) method; new implementation * Returns a string representation of this instance. */ public String toString() { return value + " " + id; } }
import java.util.Random; import java.util.Scanner; public class RandomApp { public static void main(String[] args) { int n; int[] a; Scanner in = new Scanner(System.in); StopWatch sw = new StopWatch(); System.out.print("How many random integers? "); n = in.nextInt(); a = new int[n]; Random r = new Random(); sw.reset(); for(int i = 0; i < a.length; i++) { a[i] = r.nextInt(100); } double tm = sw.elapsedTime(); System.out.printf("Time to initialize array of %d random integers: %f (msec)\n", n, tm); sw.reset(); double m = Stats.mean(a); double sd = Stats.stddev(a); tm = sw.elapsedTime(); System.out.printf("For %d random values: mean = %f, stddev = %f\n", n, m, sd); System.out.printf("Elapsed time to calculate mean and stddev: %f (msec)\n", tm); } }