Java Reference
In-Depth Information
After this statement executes, the objects and static members are as shown in Figure 8-17.
y 1
count 2
illusObject1
x
3
illusObject2
x
5
FIGURE 8-17
illusObject1
and
illusObject2
after the statement
Illustrate.count++;
executes
The output of the statements:
System.out.println(illusObject1);
System.out.println(illusObject2);
is:
x = 3, y = 1, count = 2
x = 5, y = 1, count = 2
The program in Example 8-7 further illustrates how
static
members of a class work.
8
EXAMPLE 8-7
public class
StaticMembers
{
public static void
main(String[] args)
{
Illustrate illusObject1 =
new
Illustrate(3);
//Line 1
Illustrate illusObject2 =
new
Illustrate(5);
//Line 2
Illustrate.incrementY();
//Line 3
Illustrate.count++;
//Line 4
System.out.println("Line 5: illusObject1: "
+ illusObject1);
//Line 5
System.out.println("Line 6: illusObject2: "
+ illusObject2);
//Line 6
System.out.println("Line 7: ***Increment y "
+ "using illusObject1***");
//Line 7
illusObject1.incrementY();
//Line 8
illusObject1.setX(8);
//Line 9
System.out.println("Line 10: illusObject1: "
+ illusObject1);
//Line 10
Search WWH ::
Custom Search