Previous | Table of Contents | Next |
Objects are created using an expression containing the new keyword. Creating an object from a class definition is also known as instantiation; thus, objects are often called instances.
In Java, newly created objects are allocated within an area of system memory known as the heap. All objects in Java are accessed via object referencesany variable that appears to hold an object actually contains a reference to that object. Object references are null when they do not reference any object.
Most of the time, you can be imprecise in the distinction between actual objects and references to objects. You can say pass the object to the method when you really mean pass an object reference to the method. We are careful about this distinction only where it makes a difference. Most of the time, you can use object and object reference interchangeably.
Getting back to the Point class defined previously, suppose you are building a graphics application in which you need to track lots of points. You represent each point by its own concrete Point object. Here is how you might create and initialize Point objects:
Point lowerLeft = new Point (); Point upperRight = new Point (); Point middlePoint = new Point (); lowerLeft.x = 0.0; lowerLeft.y = 0.0; upperRight.x = 1280.0; upperRight.y = 1024.0; middlePoint.x = 640.0; middlePoint.y = 512.0;
Each Point object is unique and has its own copy of the x and y fields. Changing x in the lowerLeft, for example, does not affect the value of x in upperRight object. The fields in objects are known as instance variables, because there is a unique copy of the field in each object (instance) of the class.
Per-object fields are usually what you need. You usually want a field in one object to be distinct from the similarly named field in every other object instantiated from that class.
Sometimes, though, you want fields that are shared among all objects of that class. These shared variables are known as class variablesvariables specific to the class as opposed to objects of the class.
Why would you want to use class variables? Consider the Sony Walkman factory. Each Walkman has a unique serial number. In object terms, each Walkman object has its own unique serial number field. However, the factory needs to keep a record of the next serial number to be assigned. You dont want to keep that number with every Walkman objectyoud keep only one copy of that number in the factory, or in object terms, as a class variable.
In Java, you obtain class-specific fields by declaring them static, and they are therefore sometimes called static fields. For example, a Point object to represent the origin might be common enough that you should provide it as a static field in the Point class:
public static Point origin = new Point ();
If this declaration appears inside the declaration of the Point class, there will be exactly one piece of data called Point.origin that always refers to an object at (0,0). This static field is there no matter how many Point objects are created, even if none is created. The values of x and y are zero because that is the default for numeric fields that are not explicitly initialized to a different value.
You saw one static object in your first program. The System class is a standard Java class that has a static field named out for printing output to the standard output stream.
When you see field in this chapter, it generally means a per-object field, although the term non-static field is sometimes used for clarity.
After creating an object using new, how do you get rid of the object when you no longer want it? The answer is simple: You dont. Unused Java objects are automatically reclaimed by a garbage collector. The garbage collector runs in the background and tracks object references. When an object no longer has any reference, it can be removed from the storage allocation heap, although its actual removal may be delayed until a propitious time.
Objects of the Point class as defined above are exposed to manipulation by any code that has a reference to a Point object, because its fields are declared public. The Point class is an example of the very simplest kind of class. Indeed, some classes are this simple, when they are designed to fit purely internal needs for a package, or when simple data containers are all you need.
The real benefits of object orientation, however, come from hiding the implementation of a class behind operations performed on its internal data. In Java, operations of a class are declared via its methodsinstructions that operate on an objects data to obtain results. Methods access internal implementation details that are otherwise hidden from other objects. Hiding data behind methods so that it is inaccessible to other objects is the fundamental basis of data encapsulation.
Methods have zero or more parameters. A method can return a value, or it can be declared void to indicate that it does not return any value. A methods statements appear in a block of code between curly braces { and } that follow the methods name and the declaration of its signature. The signature is the name of the method and the number and types of the methods parameters. If we enhance the Point class with a simple clear method, it might look like this:
public void clear() { x=0; y=0; }
The clear method has no parameters, hence the empty ( and ) after its name; in addition, clear is declared void because it does not return any value. Inside a method, fields and other methods of the class can be named directlywe can simply say x and y without an explicit object reference.
Previous | Table of Contents | Next |