Java Reference
In-Depth Information
class CreateSpheres {
public static void main(String[] args) {
System.out.println("Number of objects = " + Sphere.getCount());
Sphere ball = new Sphere(4.0, 0.0, 0.0, 0.0);
// Create a
sphere
System.out.println("Number of objects = " + ball.getCount());
Sphere globe = new Sphere(12.0, 1.0, 1.0, 1.0);
// Create a
sphere
System.out.println("Number of objects = " + Sphere.getCount());
// Output the volume of each sphere
System.out.println("ball volume = " + ball.volume());
System.out.println("globe volume = " + globe.volume());
}
Directory "Create Spheres"
This file should be in the same directory as the file containing the
Sphere
class definition. Compile the
source files and then run
CreateSpheres
, and you should get the following output:
Number of objects = 0
Number of objects = 1
Number of objects = 2
ball volume = 267.94666666666666
globe volume = 7234.559999999999
This is the first time you have run a program involving two source files. If you are using the JDK com-
piler, then compile
CreateSpheres.java
with the current directory as
Create Spheres
using the com-
mand
javac CreateSpheres.java
The compiler finds and compiles the
Sphere.java
source file automatically. If all the source files for a
program are in the current directory then compiling the file containing a definition of
main()
compiles
all the source files for the program.
Note that by default the
.class
files generated by the compiler are stored in the current directory — that
is, the directory containing your source code. If you want the
.class
files stored in a different directory
then you can use the
-d
option with the Java compiler to specify where they should go. For example, to
store the class files in a directory called
C:\classes
, you type:
javac -d C:/classes CreateSpheres.java
How It Works