Methods


Overview Of The Java Class Construct

This section presents an overview of the Java class construct. Java classes consist of field and method members. This section explains the purpose of these class members and prepares you for a more detailed discussion of class concepts in later sections. You have already been exposed to the structure of a Java application class in chapter 6 so some of this material will be a review.

Four Categories Of Class Members

A Java class consists of field and method members. A field is a variable or constant that represents a class or instance attribute. Fields are used to set and maintain object state information. Fields can be static or non-static. Methods can be static (like the main() method) or non-static as well. The following sections describe static and non-static fields and methods in more detail.

Static or Class-Wide Fields

A static field represents an attribute that is shared among all object instances of a particular class. This means that the field values are maintained outside of any particular instance and therefore do not require a reference to an object to access. Another term used to describe static fields is class or class-wide fields. Good candidates for static fields are class constants. If you examine the Java API classes you will see wide-spread use of class constants.

Non-Static or Instance Fields

Non-static fields represent attributes for which each object has their very own copy. Another term used to describe non-static fields is instance fields. It’s through the use of instance fields that objects can set and maintain their attribute state information. For example, if we are talking about Person objects, each Person object will have its own first name, middle name, last name, gender, and birthdate. This instance attribute state information is not shared with other Person objects. Figure 9-3 graphically illustrates the relationship between static and non-static fields.

image from book
Figure 9-3: Static and Non-Static Fields

Static or Class-Wide Methods

A static method can be called via the class name and has direct access only to a class’s static fields. Static methods must access instance fields via an object reference. You are already familiar with the main() method and in chapter 6 you saw how the main() method accessed static and non-static fields. You will very rarely, if ever, write a static method other than main(). If you do write a static method it will usually be for a very specialized purpose. (See the Singleton and Factory patterns discussed in chapter 25.) You will see a static method in action when I show you how to use the Calendar class.

Non-Static or Instance Methods

A non-static method enjoys direct access to a class’s static and non-static fields. Another name for a non-static method is instance method. Instance methods must be accessed or called via an object reference, hence the name instance method. The methods you create will overwhelmingly be non-static methods.

Access Modifiers

The access modifiers public, protected, and private are used to control access to fields and methods. If no access is specified then default or package access is implied.

public

The keyword public indicates that the class, field, or method is accessible to all clients. Generally speaking, most of the classes, and the methods you declare in a class, will be public. Fields, when intended to be used as class-wide constants, will often be declared public as well.

private

The keyword private indicates that the field or method is intended for internal class use only and is not available for use by client programs. You will usually declare non-static instance fields to be private. You can think of private fields as being surrounded by the protective cocoon of the class.

Methods are often declared to be private as well. Private methods are intended to be utilized exclusively by other methods within the class. These private methods are often referred to as utility methods since they are usually written to perform some utility function that is not intended to be part of the class interface.

protected

The keyword protected is used to prevent horizontal access to fields and methods but allow fields and methods to be inherited by subclasses. The protected keyword is covered in detail in chapter 11.

package

If a class, field, or method is not declared to be public, private, or protected then it has default or package accessibility. Package accessibility is discussed in detail in chapter 11 as well.

The Concepts Of Horizontal Access, Interface, and Encapsulation

The term horizontal access is used to describe the access a client object has to the methods and fields of a server object. The client object represents the code that uses the services of another object. It can do this in two ways: 1) by accessing a class’s public static methods or fields, or 2) by creating an instance of the class and accessing its public non-static fields and methods via an object reference.

The fields and methods a class exposes as public are collectively referred to as its interface. Client code becomes dependent upon these interface fields and methods. The wrong kind of change to a class’s interface will break any code that depends upon that interface. When changing a class’s interface the rule-of-thumb is that you can add public fields and methods but they can never be removed. If you look through the Java API you will see lots of classes with deprecated methods. A deprecated method is a method that is targeted for deletion in some future version of the API. These methods are not yet removed because doing so would break existing programs that utilize (depend upon) those methods.

Any field or method declared private is said to be encapsulated within their class since they are shielded from horizontal access by client code. Generally speaking, a class’s interface can be thought of as the set of services it provides to client programs. It provides those services by manipulating its private, or encapsulated, data structures. The idea is that at some point in the future a programmer may think up a new way to deliver a particular service’s functionality. They may do this by making changes to the class’s internal, or private, data structures. Since these data structures are encapsulated, a change to them will have no effect on client code, except perhaps for the effects of an improvement to the service provided.

Figure 9-4 illustrates the concept of horizontal access and the effects of using public and private access modifiers.

image from book
Figure 9-4: Horizontal Access Controlled via Access Modifiers public and private

The concepts of interface, horizontal access, and encapsulation are important to the world of object-oriented programming which means they are important to you. You will deal with these issues every time you write code in the Java language.

Quick Review

Java classes have four types of members: 1) static fields, 2) non-static fields, 3) static methods, and 4) non-static methods. Static fields are shared between all class objects whereas each object has its very own copy of instance fields. Alternative names for static are class and class-wide. An alternative name for non-static is instance. Static methods can only directly manipulate static fields. Non-static methods can manipulate both static and non-static fields.

There are three access modifiers: public, private, and protected. All three access modifiers are used to control horizontal access. If no access is specified the default or package access is implied.

Horizontal access is the term used to describe how a client object uses the services of a server object. Public fields and methods published by a class are collectively referred to as its interface. Private fields and methods are said to be encapsulated within the class. Client code becomes dependent upon a class’s interface. Changes to a class’s interface can potentially break any client code that depends upon its interface.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net