Every Java program consists of a collection of classes--nothing else. A class is a template for creating a particular form of object. Each object created by the 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.
A member of a class has an identifier (defined above in Section 1.2.4) as its name. 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.11.4. Each member name must conform to the syntactic rules given for variables in Section 1.2.4.
Java objects are a generalization of Scheme structs, because the collection of operations (methods) that operate on the instances of a class is determined by the class definition. In Scheme, the set of operations for manipulating a struct is fixed (a constructor, a recognizer, field-accessors, and field-modifiers).
The following Java program text defines a class Entry suitable for representing entries in the department directory program introduced in Section :
This class is the Java analog of the Scheme structure definitionclass 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; } }
where the name, address and phone fields are always bound to strings.(define-struct Entry (name address phone))
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
The expression is equivalent to the Scheme expressionnew Entry("SomeName","SomeAddress","SomePhone")
which creates a Scheme Entry object. The three accessor methods getName, getAddress, and getPhone are equivalent to the Scheme accessors Entry-name, Entry-address and Entry-phone that are automatically generated by the Scheme define-struct definition.(make-Entry "SomeName" "SomeAddress" "SomePhone")
Recall the syntax for method invocation in Java is quite different from the syntax for functional application in Scheme. Given an instance e of class Entry, the expression
e.getName()invokes the getName method of object e. The equivalent Scheme expression is written
(Entry-name e)
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 (non-static) method invocation. For example, the expression
this.namereturns the value of the name field of the object this. In constructor invocations, this is bound to the newly allocated object.
Finger Exercise: In the Definitions window of DrJava,
enter the Java program text given above defining the Entry
class.
In the DrJava Interactions window, try
evaluating the following program text:
Save your program for future use in a file named entry.java.Entry e = new Entry("Corky", "DH 3104", "x 6042"); e.getName() e.getAddress() e.getPhone()
We will explain the syntax of Java class definitions
in more detail in Section ??.