In this section, we will examine the distinction between object and class properties.
104 Object-Oriented Programming and Java
8.2.1 Counting Instances
Listing 8-1 contains the code for an example that counts the number of objects instantiated from the SalesPerson class.
Listing 8-1: Counting instances.
The code begins with the declaration of a variable count. This variable is used to continually create the number of SalesPerson objects. For each creation of a SalesPerson object, count is incremented via the statement:
count = count+1;
This statement is executed twice since two SalesPerson objects were instanti- ated. Finally, the code prints out the number of SalesPerson objects instantiated via the statement
The output:
suggests that two SalesPerson objects were created, and this is clearly correct.
While the solution is correct, it is cumbersome since for each new instantiation of SalesPerson object, a fresh
count = count+1;
statement has to be added into main().
An alternate class organisation is given in Listing 8-2, where count is incre- mented within the constructor method of the SalesPerson class. This strategy sup- ports abstraction and is advantageous because the user does not have to bother with operations on count.
class SalesPerson { String employeeId;
SalesPerson(String aEmployeeId) { employeeId = aEmployeeId;
}
public static void main(String arg[]) { int count = 0;
count = count+1;
count = count+1;
} }
SalesPerson s1 = new SalesPerson("12345S");
SalesPerson s2 = new SalesPerson("33221K");
System.out.println(count + " salespersons have been created");
System.out.println(count + " salespersons have been created");
"2 salespersons have been created"
Modularity 105
Listing 8-2: Alternative solution to counting instances.
It is clear from the output:
”1 salespersons have been created”
that the result is incorrect.1
The variable count in the alternative solution is declared as an object attribute rather than a local variable of the static method main() as was the case for the previ- ous solution. As an object attribute, count can now be incremented in the constructor method of the SalesPerson class. However, with each instantiation of a SalesPerson object, the count variable of each newly created object is incremented. Since two instances of SalesPerson object were created, two independent copies of count, each having the value 1, were present. Figure 8-1 shows the state of the two created SalesPerson objects.
SalesPerson s1 Attributes
- employeeNumber : 12345S - count : 1
Operations - getCount()
SalesPerson s2 Attributes
- employeeNumber : 33221K - count : 1
Operations - getCount()
Figure 8-1: State of SalesPerson objects.
1Even if s1.getCount() has been substituted with s2.getCount() in the println() method, the result will still be incorrect.
class SalesPerson { String employeeId;
int count = 0;
SalesPerson(String aEmployeeId) { employeeId = aEmployeeId;
count = count + 1;
}
int getCount() { return count; }
public static void main(String argv[]) { SalesPerson s1 = new SalesPerson(”12345S”);
SalesPerson s2 = new SalesPerson(”33221K”);
System.out.println(s1.getCount() +
” salespersons have been created”);
} }
106 Object-Oriented Programming and Java
Although only one copy of count is required, it is unclear which copy of the two instances should be used.
8.2.2 Shared Attributes
Another solution can be found in Listing 8-3 whereby count is declared as static. Declaring count as static allows the variable count to be shared among all in- stances of the SalesPerson class. Thus, s1.count refers to the same memory location as s2.count. The statement:
count = count+1;
in the constructor method therefore increments the same shared copy of count as shown in Figure 8-2. The output from the code:
”2 salespersons have been created”
is correct.
Listing 8-3: Shared attributes.
SalesPerson s1 Attributes
- employeeNumber : 12345S Operations
- getCount() SalesPerson Attributes - count : 2
SalesPerson s2 Attributes
- employeeNumber : 33221K Operations
- getCount()
Figure 8-2: Shared variable count.
class SalesPerson { String employeeId;
static int count = 0;
SalesPerson(String aEmployeeId) { employeeId = aEmployeeId;
count = count + 1;
}
int getCount() { return count; }
public static void main(String argv[]) {
System.out.println(s1.getCount() + }
}
SalesPerson s1 = new SalesPerson("12345S");
SalesPerson s2 = new SalesPerson("33221K");
" salespersons have been created");
Modularity 107
8.2.3 Class Attributes
The static variable count is also known as a class attribute. While a class defini- tion specifies the structure and behavior of objects within, it may have its own attri- bute and method definitions.
An attribute definition that is preceded with the keyword static is a class attribute. While we previously viewed the static variable count as shared amongst all instances, its association with the class is consistent. As a class attribute, it is also accessible to instances of the class.
8.2.4 Class Methods
In another change in Listing 8-4, we make getCount() a class method by prefixing it with the static keyword. The results from the code is the same as that of Listing 8-3 with just staticcount.
Listing 8-4: Class methods.
SalesPerson s1 Attributes
- employeeNumber : 12345S SalesPerson
Attributes - count : 2 Operations - getCount()
SalesPerson s2 Attributes
- employeeNumber : 33221K
Figure 8-3: Class Method getCount() class SalesPerson {
String employeeId;
static int count = 0;
SalesPerson(String aEmployeeId) { employeeId = aEmployeeId;
count = count + 1;
}
static int getCount() { return count; } public static void main(String argv[]) { SalesPerson s1 = new SalesPerson("12345S");
SalesPerson s2 = new SalesPerson("33221K");
System.out.println(s1.getCount() +
" salespersons have been created");
} }
Note the difference in representation between the getCount() method of Figure 8-3 for Listing 8-4 and Figure 8-2 for Listing 8-3. In Figure 8-3, both SalesPerson to the SalesPerson class as represented by the outermost bubble surrounding instances s1 and s2.
objects s1 and s2 do not own the getCount() method since the method belongs
108 Object-Oriented Programming and Java
8.2.5 Name Aliases
(where there may be several getCount() methods in other class definitions), quali- fication by the class name as in SalesPerson.getCount() is the only way to access the method. Thus, within staticvoidmain(), the println() method could have been: