Referring to Class Elements

Classes become useful when they work together to build more complex functionality. Java classes often contain references to other classes.

The fields and methods of these referenced classes are accessed using the standard dot notation used in most OOP languages. If a Circle class has a borderColor field and a getArea() method that are publicly accessible, another class with a Circle instance named myCircle could access them as shown in the following:

myCircle.borderColor = Color.blue;
double area = myCircle.getArea();

You have seen how to refer to other classes but how does a class refer to itself? Although the reasons to do so might not seem so obvious at first, self-reference by a class is an important capability. ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.