first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

Core Java 1.1 Volume I --- Fundamentals: Chapter 4, Objects and Classes

Objects

Data is encapsulated in an object. The data is stored inside the object as "instance variables" or "instance fields." If the access modifier "private" is used when creating instance fields, then the principle of "encapsulation" is enforced. The particular values of the instance fields in an object constitutes the "state" of the object.

To enforce encapsulation, object methods should be used to handle data. Object methods are used to associate "behavior" with an object. Methods are defined with respect to their relationship to object data (instance fields). It follows that there are "mutator methods" and "accessor methods," where, respectively, object data is changed and object data is retrieved. The convention in Java is to use the lowercase prefix "get" for accessor methods and "set" for mutator methods.

Classes

Objects come from classes. A class contains a "code block" punctuated with braces ("{" and "}"). This code block contains methods and instance fields. The people at Sun recommend that class methods appear first followed by instance fields (this is meant to show an understanding of the ideal of encapsulation). The opinion here is that there are three types of classes: (i) classes that make up the current Java standard or other third-party pre-packaged classes; (ii) user-defined classes that manipulate data; and (iii) classes that manipulate user-defined classes (as well as manipulating data).

The last type of class almost always contains a method called "main()" returning "void." The main() method is almost always used instantiate other classes. In a simple "hub and spokes" application design, the class with the main() method calls classes without a main() method. Classes without a main() method may, at least, contain three items: (i) a private data field, (ii) a public field accessor method and (iii) a public field mutator method.

When the class is instantiated by the "new" keyword, its methods are available as long as they are public. When a class has one or more methods with the same name as the class, these methods are called "constructors." A single class can have multiple constructor methods via "overloading."

A class can have "private" methods. These methods are often used to break up a large procedure into one public method and several private ones.

A class can have "static" methods. These methods "belong to the class" and not to any particluar instance of the class. It follows that static methods can only access the static fields of an object. Recall that the main() method is static. Becuase of this, it is not uncommon for main() to create an object of its own class to access its instance fields via an object variable (see p. 137).

When a class has method called "finalize()" this method will be called whenever the Java system decides to perform "garbage collection."

mod date: 2000-09-16T14:28:00.000Z