Java Reference
In-Depth Information
1.7.1. Creating Objects
Objects are created by expressions containing the
new
keyword. Creating
an object from a class definition is also known as
instantiation;
thus, ob-
jects are often called
instances.
Newly created objects are allocated within an area of system memory
known as the
heap.
All objects are accessed via
object references
any
variable that may appear to hold an object actually contains a reference
to that object. The types of such variables are known as
reference types,
in contrast to the primitive types whose variables hold values of that
type. 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 meth-
od." We are careful about this distinction only when it makes a differen-
ce. Most of the time, you can use "object" and "object reference" inter-
changeably.
In the
Point
class, 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;