6.2 Objects and Classes Revisited | 273
(^1) It is an instance method.
In Chapter 2, we saw that a class declaration is a collection of field and method
declarations.The fields and methods in a class are called themembersof the class.
As we have also seen, members can bepublicorprivate, and they can bestatic(or
notstatic) andfinal(or notfinal). Thepublicmembers of a class combine to
specify itspublic interface. Here we useinterfacein the general sense of the word, in
contrast to the Javainterfaceconstruct, which we introduce later. When a pro-
grammer wishes to employ a predefined class in writing a program, he or she
looks up its interface to see what fields and methods it makes available for use.
What do we mean by “modes of access” in our definition of interface? In
Chapter 2, we defined the access modifiers as reserved words and explained their
effects. Here we are interested in how they relate to the public interface of a class.
Recall our earlier discussion of instance methods and class methods. We invoke a class
method by writing the class name, a period, and then the method name. We invoke an in-
stance method by writing the object name, a period, and then the method name. In a class
declaration, the two types of methods look exactly the same except that class method dec-
larations include the staticmodifier. In other words, the staticmodifier specifies this as-
pect of how we access the method. Consider these statements:
value = Double.parseDouble(inFile.readLine());
System.out.println("Value input is: "+ value);
parseDoubleis a class method and is prefaced by the name of the class Double. It takes a string
and converts it into a value of type double.readLineis an instance method and is prefaced by
inFile, an instance of the class BufferedReader. The third method call in these two state-
ments is a call to println. From its call, can you tell whether printlnis an instance method
or a class method?^1
The publicand privatemodifiers determine whether a member can be accessed outside
of a class. The finalmodifier specifies whether a member can be changed. There are eight
combinations of these modifiers, each of which effectively defines a mode of access. Only
the publicmembers are part of the public interface, however. From code that is outside of the
class definition, we cannot access members that are private.
For example, the public interface for the class Doubleincludes some finalfields, such as
the maximum and minimum doublevalues. It provides a set of class (static) methods that
includes parseDouble, and a set of instance methods that includes a version of compareTo.All
of these members are public. We can also surmise that the definition of Doubleincludes a pri-
vatefield of type doublethat is used to store a numerical value. Later in this chapter and in
Chapter 7, we introduce additional access modifiers.
Member A field or method
within a class
Public interface The members
of a class that can be accessed
outside of the class, together
with the modes of access that
are specified by other modifiers