Java Reference
In-Depth Information
Accessing Variables and Methods
You may want to access fields and methods that are defined within a class from outside it. It is considered
bad practice to make the fields available directly but there can be exceptions to this — you'll find some in
the standard libraries. You'll see later that it is possible to declare class members with restrictions on ac-
cessing them from outside, but let's cover the principles that apply where the members are accessible. I'll
consider accessing static members — that is, static fields and methods — and instance members separately.
You can access a static member of a class using the class name, followed by a period, followed by the
member name. With a class method you also need to supply the parentheses enclosing any arguments to the
method after the method name. The period here is called the dot operator. So, if you want to calculate the
square root of π you can access the class method sqrt() and the class variable PI that are defined in the
Math class as follows:
double rootPi = Math.sqrt(Math.PI);
This shows how you call a static method — you just prefix it with the class name and put the dot operator
between them. You also reference the static data member, PI , in the same way — as Math.PI . If you have
a reference to an object of a class type available then you can also use that to access a static member of the
class because every object always has access to the static members of its class. You just use the variable
name, followed by the dot operator, followed by the member name.
Of course, as you've seen in previous chapters, you can import the names of the static members of the
class by using an import statement. You can then refer to the names of the static members you have impor-
ted into your source file without qualifying their names at all.
Instance variables and methods can be called only using an object reference, because by definition they
relate to a particular object. The syntax is exactly the same as I have outlined for static members. You put
the name of the variable referencing the object followed by a period, followed by the member name. To use
a method volume() that has been declared as an instance method in the Sphere class, you might write:
double ballVolume = ball.volume();
Here the variable ball is of type Sphere and it contains a reference to an object of this type. You call its
volume() method, which calculates the volume of the ball object, and the result that is returned is stored
in the variable ballVolume .
Final Fields
You can declare a field in a class to be final , which means that the field cannot be modified by the methods
in the class. You can provide an initial value for a final field when you declare it. For example:
final double PI = 3.14;
If you don't provide an initial value for a final field when you declare it, you must initialize it in a con-
structor , which you learn about a little later in this chapter.
DEFINING CLASSES
Search WWH ::




Custom Search