Java Reference
In-Depth Information
System.out.println("Line 11: illusObject2: "
+ illusObject2);
//Line 11
System.out.println("Line 12: ***Increment y "
+ "using illusObject2***");
//Line 12
illusObject2.incrementY();
//Line 13
illusObject2.setX(23);
//Line 14
System.out.println("Line 15: illusObject1: "
+ illusObject1);
//Line 15
System.out.println("Line 16: illusObject2: "
+ illusObject2);
//Line 16
}
}
Sample Run:
Line 5: illusObject1: x = 3, y = 1, count = 1
Line 6: illusObject2: x = 5, y = 1, count = 1
Line 7: ***Increment y using illusObject1***
Line 10: illusObject1: x = 8, y = 2, count = 1
Line 11: illusObject2: x = 5, y = 2, count = 1
Line 12: ***Increment y using illusObject2***
Line 15: illusObject1: x = 8, y = 3, count = 1
Line 16: illusObject2: x = 23, y = 3, count = 1
The preceding program works as follows: The
static
data members
y
and
count
are
initialized to
0
. The statements in Lines 1 and 2 create the
Illustrate
objects
illusObject1
and
illusObject2
. The instance variable
x
of
illusObject1
is initi-
alized to
3
; the instance variable
x
of
illusObject2
is initialized to
5
.
The statement in Line 3 uses the name of the
class
Illustrate
and the method
incrementY
to increment
y
. Because
count
is a
public static
member of the
class
Illustrate
, the statement in Line 4 uses the name of the
class
Illustrate
to
directly access
count
, and increments it by
1
. The statements in Lines 5 and 6 output
the data stored in the objects
illusObject1
and
illusObject2
. Note that the value of
y
for both objects is the same. Similarly, the value of
count
for both objects is the same.
The statement in Line 7 is an output statement. The statement in Line 8 uses the object
illusObject1
and the method
incrementY
to increment
y
. The statement in Line 9 sets
the value of the instance variable
x
of
illusObject1
to
8
. Lines 10 and 11 output the data
stored in the objects
illusObject1
and
illusObject2
. Note that the value of
y
for both
objects is the same. Similarly, the value of
count
for both objects is the same. Moreover,
notice that the statement in Line 9 only changes the value of the instance variable
x
of
illusObject1
because
x
is not a
static
member of the
class
Illustrate
.
The statement in Line 13 uses the object
illusObject2
and the method
incrementY
to
increment
y
. The statement in Line 14 sets the value of the instance variable
x
of
illusObject2
to
23
. Lines 15 and 16 output the data stored in the objects
illusObject1
and
illusObject2
. Notice that the value of
y
for both objects is the
Search WWH ::
Custom Search