Terminology
Class vs. Object
We create an Object in a program as it executes, and call methods on it. A class is a Java construct “public class … “ that allows us to define new types of objects as well as client code for them.
Object instance: same as object. Also instance = object.
Object state: set of values stored in an object
Field: a variable declared in a class outside any methods (and without “static”). It holds part of object state. Thus the set of fields in an object class define the object state.
Method: two kinds now, object methods (or instance methods, with no static keyword), and static methods. Instance methods provide actions on their objects, while static methods just provide actions on whatever they are given.
Method calls: To call an object method, you need a reference to the object you want to work with, then dot, then name of object method and params. To call a static method, you need the name of the method plus params. If the static method is in another class, you need the class name dot the method name, as in Math.max(x, y). So the existence of a dot does not guarantee a call is a call to an object method, but if it starts with a lowercase letter, as in “p1.getX()”, it’s usually an object call, since class names are almost always capitalized.
More on methods: Mutators and Accessors
Constructors: code in an object class for creating objects. Not strictly speaking a method, but close to it. Constructors have special syntax—must be named the same as the class, and have no return type.
PhoneEntry Example—online demo using posted code. Here we are using Strings for object state, so we actually have objects within objects, but Strings are so well behaved it works very easily.
Class Exercise—adding a method to PhoneEntry.