Class Fundamentals in Java
Class Fundamentals in Java
Class Fundamentals in Java
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
The data, or variables, defined within a class are called
instance variables. The code is
contained within methods. Collectively, the methods and
variables defined within a class
are called members of the class. In most classes, the instance
variables are acted upon and
accessed by the methods defined for that class. Thus, as a
general rule, it is the methods
that determine how a class data can be used.
Variables defined within a class are called instance variables
because each instance of
the class (that is, each object of the class) contains its own
copy of these variables. Thus, the
data for one object is separate and unique from the data for
another. We will come back to
this point shortly, but it is an important concept to learn early.
All methods have the same general form as main( ), which we
have been using thus far.
However, most methods will not be specified as static or
public. Notice that the general
form of a class does not specify a main( ) method. Java classes
do not need to have a main( )
method. You only specify one if that class is the starting point
for your program. Further,
some kinds of Java applications, such as applets, dont require a
main( ) method at all.
A Simple Class
Lets begin our study of the class with a simple example. Here
is a class called Box that
defines three instance variables: width, height, and depth.
Currently, Box does not contain
any methods (but some will be added soon).
class Box {
double width;
double height;
double depth;
}
As stated, a class defines a new type of data. In this case, the
new data type is called Box. You
will use this name to declare objects of type Box. It is
important to remember that a class
declaration only creates a template; it does not create an
actual object. Thus, the preceding
code does not cause any objects of type Box to come into
existence.
Box mybox = new Box(); // create a Box object called mybox
After this statement executes, mybox will be an instance of
Box. Thus, it will have physical
reality. For the moment, dont worry about the details of this
statement.
As mentioned earlier, each time you create an instance of a
class, you are creating an
object that contains its own copy of each instance variable
defined by the class. Thus, every
Box object will contain its own copies of the instance variables
width, height, and depth. To
access these variables, you will use the dot (.) operator. The dot
operator links the name of
the object with the name of an instance variable. For example,
to assign the width variable
of mybox the value 100, you would use the following
statement:
mybox.width = 100;
This statement tells the compiler to assign the copy of width
that is contained within the
mybox object the value of 100. In general, you use the dot
operator to access both the
instance variables and the methods within an object. One other
point: Although commonly
referred to as the dot operator, the formal specification for Java
categorizes the . as a separator.
However, since the use of the term dot operator is
widespread, it is used in this book.
Here is a complete program that uses the Box class:
/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
You should call the file that contains this program
BoxDemo.java, because the main( )
method is in the class called BoxDemo, not the class called
Box. When you compile this
program, you will find that two .class files have been created,
one for Box and one for
BoxDemo. The Java compiler automatically puts each class
into its own .class file. It is not
necessary for both the Box and the BoxDemo class to actually
be in the same source file.
You could put each class in its own file, called Box.java and
BoxDemo.java, respectively.
To run this program, you must execute BoxDemo.class. When
you do, you will see the
following output:
Volume is 3000.0
As stated earlier, each object has its own copies of the instance
variables. This means
that if you have two Box objects, each has its own copy of
depth, width, and height. It is
important to understand that changes to the instance variables
of one object have no