Java Reference
In-Depth Information
3.2.3
Referencing components
You already know how to reference a component, but we repeat the information
here for completeness. Suppose v contains the name of some folder of class
Employee . Then we call a method like getStart that is in the folder using:
v.setStart( arguments );
Of course, the method must be public in order to do this outside the class.
Actually, v can be any expression whose value is a (suitable) folder; it need
not be a variable. For example, above, we assigned the current year to variable
year using this assignment statement:
Activities
3-3.2, 3-6.1
int year= ( new Date()).getYear() + 1900;
Here, new Date() creates a folder of class Date and yields the folder's name a3
(say); so we have a3.getYear() . This calls function getYear of folder a3 .
In the same way, if expression e yields the name of a folder and the folder
has a field name , then e.name refers to that field.
3.2.4
Equality testing and aliasing
Consider the following two statements:
Lesson page 3-
7 discusses this
topic.
Employee b= new Employee("Gries", 1966);
Employee c= new Employee("Gries", 1966);
These two statements store in variables b and c the names of two different fold-
ers, whose contents happens to be the same. The folders and variables after the
assignments are shown in Fig. 3.4.
In this situation, the relation
b a4
c a5
a4
a5
Employee
Employee
getName()
setName(String)
getStart()
setStart( int )
getCompensation()
addToSalary( double )
toString()
Employee()
getName()
setName(String)
getStart()
setStart( int )
getCompensation()
addToSalary( double )
toString()
Employee()
Gries
Gries
name
name
1966
1966
start
start
50000.
salary
salary
50000.
Figure 3.4:
Variables b and c and the folders whose names they contain
Search WWH ::




Custom Search