Objects and Classes in Java |
Following is the code for a class calledSimplePoint
that represents a point in 2D space:This segment of code declares a class-- a new data type really-- calledpublic class SimplePoint { public int x = 0; public int y = 0; }SimplePoint
. TheSimplePoint
class contains two integer member variables,x
andy
. Thepublic
keyword preceding the declaration forx
andy
means that any other class can freely access these two members.You create an object from a class such as
Now, here's a class,SimplePoint
by instantiating the class. When you create a newSimplePoint
object (we show you how shortly), space is allocated for the object and its membersx
andy
. In addition, thex
andy
members inside the object are initialized to 0 because of the assignment statements in the declarations of these two members.SimpleRectangle
, that represents a rectangle in 2D space:This segment of code declares a class (another data type)--public class SimpleRectangle { public int width = 0; public int height = 0; public SimplePoint origin = new SimplePoint(); }SimpleRectangle
-- that contains two integer members,width
andheight
.SimpleRectangle
also contains a third member,origin
, whose data type isSimplePoint
. Notice that the class nameSimplePoint
is used in a variable declaration as the variable's type. You can use the name of a class anywhere you can use the name of a primitive type.Just as
width
"is an" integer andheight
"is an" integer,origin
"is a"SimplePoint
. On the other hand, aSimpleRectangle
object "has a"SimplePoint
. The distinction between "is a" and "has a" is critical because only an object that "is a"SimplePoint
can be used where aSimplePoint
is called for.As with
This diagram shows the difference between primitive types and reference types. BothSimplePoint
, when you create a newSimpleRectangle
object, space is allocated for the object and its members, and the members are initialized according to their declarations. Interestingly, the initialization for theorigin
member creates aSimplePoint
object with this code:new SimplePoint()
as illustrated here:width
andheight
are integers and are fully contained withinSimpleRectangle
. On the other hand,origin
simply references aSimplePoint
object somewhere else.The
SimplePoint
andSimpleRectangle
classes as shown are simplistic implementations for these classes. Both should provide a mechanism for initializing their members to values other than 0. Additionally,SimpleRectangle
could provide a method for computing its area, and becauseSimpleRectangle
creates aSimplePoint
when it's created, the class should provide for the clean up of theSimplePoint
whenSimpleRectangle
gets cleaned up. So, here's a new version ofSimplePoint
, calledPoint
, that contains a constructor which you can use to initialize a newPoint
to a value other than (0,0):Now, when you create apublic class Point { public int x = 0; public int y = 0; // a constructor! public Point(int x, int y) { this.x = x; this.y = y; } }Point
, you can provide initial values for it like this:The values 44 and 78 are passed into the constructor and subsequently assigned to thenew Point(44, 78)x
andy
members of the newPoint
object as shown here: Now, let's beef up theSimpleRectangle
class. Here's a new version ofSimpleRectangle
, calledRectangle
, that contains four constructors, a method to "move" the rectangle, a method to compute the area of the rectangle, and a finalize method to provide for clean up:The four constructors allow for different types of initialization. You can create a newpublic class Rectangle { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { this(new Point(0, 0), w, h); } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.x = x; origin.y = y; } // a method for computing the area of the rectangle public int area() { return width * height; } // clean up! protected void finalize() throws Throwable { origin = null; super.finalize(); } }Rectangle
and let it provide default values for everything, or you can specify initial values for the origin, the width and the height, or for all three when you create the object. You'll see more of this version of theRectangle
class in the next section.This section glossed over some details and left some things unexplained, but it provides the basis you need to understand the rest of this lesson. After reading this section, you should know that
You also should have a general understanding or a feeling for the following:
- objects are created from classes
- an object's class is its type
- how "is a" differs from "has a"
- the difference between reference and primitive types.
Now, let's look in detail at the life cycle of an object, specifically how to create, use, and destroy an object.
- How to create an object from a class
- What constructors are
- What the code for a class looks like
- What member variables are
- How to initialize objects
- What methods look like
Objects and Classes in Java |