next up previous
Next: 1.1.3 Defining Constants Up: 1.1 Introduction Previous: 1.1.1 What is an


1.1.2 Classes: Templates for Creating Objects

Every Java program consists of a collection of classes--nothing else. A class is a template for creating a particular form of object. A Java class definition corresponds to a C++ struct definition generalized to include all of procedures that process objects of the defined class. In Java, all program code must be part of some class.

Each object created by a class template contains the same members, each of which is either a field or a method. A field is a ``container'' that holds a value. A method is an operation on the fields of the object and any values that are passed as arguments to the method. The objects created by a particular class template are called the instances or objects of that class. Each instance contains the members specified in the class template.

Each member of a class has a name consisting of a Java identifier, any sequence of ``alphanumeric characters'' (letters, digits, and _) beginning with a letter or _ other than a keyword. A complete list of the keywords of Java is given in Chapter 4 which is an Appendix on the syntax of Java. For now, we will require all such names to be unique within a class. We will slightly relax this restriction when we discuss overloading in Section 1.9.4.

The Java program in Figure 1 defines a class Entry suitable for representing entries in the department directory application described in Section 1.1.1:

class Entry {

  /* fields */ 
  String name;	
  String address;
  String phone;

  /* constructor */
  Entry(String n, String a, String p) {
    this.name = n;
    this.address = a;
    this.phone = p;
  }	 	 

  /* accessors */
  String getName() { return this.name; }
  String getAddress() { return this.address; }
  String getPhone() { return this.phone; }

}
Figure 1: The Entry class

Let's examine the syntax of the Entry class definition. It consists of seven members:

An instance (object) of the class Entry is created by an expression of the form

    new Entry("SomeName","SomeAddress","SomePhone")

The three methods defined in class Entry are extremely simple, yet they illustrate the most important characteristic of object-oriented programming: operations are attached to the data objects that they process. The methods getName, getAddress, and getPhone take no arguments yet they have access to the fields of the Entry object to which they are attached. The Entry object e in the method call

e.getName()
is called the receiver because it ``receives'' the method call. (In the Java Language Specification, the term target is used instead of receiver.)

In the code for the Entry class, the constructor and accessor methods all refer to fields of this, the hidden parameter bound to the object that is the receiver of the method invocation. For example, the expression

this.name
returns the value of the name field of the object this. In constructor invocations, this is bound to the newly allocated object.

One attractive feature of Java is that the method syntax mandates a type contract (declaration of input types and output type) as part of the method header. For example, the method header

String getName()
indicates that the getName method takes no arguments (other than the ``receiver'' as an implicit argument (bound to this in the body of the method) and returns a String value. In subsequent examples, we will define methods that take explicit arguments; the type of each such argument must be declared in the method header.


Finger Exercise: In the Definitions pane of DrJava, enter the Java program defining the Entry class given earlier in this section. In the DrJava Interactions pane, try evaluating the following program text:

Entry e =  new Entry("Corky", "DH 3104", "x 6042");
e.getName()
e.getAddress()
e.getPhone()
Save your program for future use in a file named Entry.java.


We will explain the syntax of Java class definitions in more detail in Section 1.3.


next up previous
Next: 1.1.3 Defining Constants Up: 1.1 Introduction Previous: 1.1.1 What is an
Corky Cartwright 2003-07-07