We declare data of the primitive data types with statements
like this:
double dvar = 1.4;
Internally, eight bytes of memory will be assigned
for the dvar
variable and filled with the floating-point
representation of 1.4. When dvar
is used in later code, the data will be retrieved from memory and
used in operations according to the double
type. Though not the terminology typically used for primitive data,
you can call dvar
an instance of the double type. A program could declare more
double variables,
each similarly representing another instance of the double
type.
A similar process occurs when creating and using an
instance (or object) of a class definition. This process is referred
to as instantiation. Lets use the following class for our
explanation of instantiation:
class
Test
{
int i;
double x;
Test (int j, double y) {
i = j;
double x = y;
}
int getInt () {
return i;
}
double getDouble () {
return x;
}
double calculate () {
return i*x;
}
}
|
When Test
class is instantiated with the new
operator, as in
Test
g1 = new Test (4, 5.1);
a set of data is created for the data fields "i"
and "x"
of the class and stored under a unique ID for that instance
or object (we use instance and object interchangeably).
If we create another instance of the class
Test
g2 = new Test(53, 34.3);
then another set of data will be stored under a new ID.
When calling (invoking) a method for an object, the data for that
object will be inserted into the method code for that class. We
often refer to objects in rather abstract or pictoral metaphors
but at the processor level it just comes down to sets of data, unique
to each object, shifting in and out of the method codes for that
class.
Object
References
The g1
and g2 variables
above are called references since they point to the instances of
a class. Java references differ considerably from C and C++ memory
pointers where the programmer can access and manipulate values directly.
Pointers in C and C++
- hold the actual addresses of data in memory.
- can be cast to different data types.
- can be altered to point to other memory locations.
A Java reference holds an indirect address of an object in the
JVM and
- the actual memory value of the reference is hidden.
- reference values cannot be altered .
- references can only be recast to a superclass or subclass of
that object, never to other data types. (See Chapter
4 for a discussion of superclasses and subclasses.)
So references in Java act in some ways like memory pointers but
are much safer with regard to creating program errors and security
problems.
Accessing
Fields and Methods
To create a useful program in Java, an object obviously needs
to invoke the methods of other objects and to access data in other
objects. This is done with a reference and the "." or dot operator.
For example, we invoke the calculate()
method for an instance of the Test
class above as follows:
Test
g1 = new Test (4, 5.1);
double z = g1.calculate ();
The invocation of calculate()
with the g1
reference will cause the data for the g1
object to be used with the bytecodes for that method.
You can also directly access the data fields as in
double
y = g1.x;
int j = g1.i;
In some situations you may not want give all other classes access
to certain data (e.g. a password value) and methods. We will discuss
access settings in Chapter
5 that allow you to determine what other classes can access the
fields and methods in a class.
Latest update: Oct. 17, 2004
|