Java Reference
In-Depth Information
Ask the Expert
Q
:
Can
final
member variables be made
static?
Can
final
be used on method para-
meters and local variables?
A
:
The answer to both is Yes. Making a
final
member variable
static
lets you refer to
the constant through its class name rather than through an object. For example, if the
constants in
ErrorMsg
were modified by
static
, then the
println( )
statements in
main( )
could look like this:
Declaring a parameter
final
prevents it from being changed within the method. Declaring
a local variable
final
prevents it from being assigned a value more than once.
The Object Class
Java defines one special class called
Object
that is an implicit superclass of all other
classes. In other words, all other classes are subclasses of
Object
. This means that a refer-
ence variable of type
Object
can refer to an object of any other class. Also, since arrays are
implemented as classes, a variable of type
Object
can also refer to any array.
Object
defines the following methods, which means that they are available in every ob-
ject:
Method
Purpose
Creates a new object that is the same as the object
being cloned.
Object clone( )
boolean equals(Object
object
)
Determines whether one object is equal to another.
void finalize( )
Called before an unused object is recycled.
Class<?> getClass( )
Obtains the class of an object at run time.
Returns the hash code associated with the invoking
object.
int hashCode( )
Resumes execution of a thread waiting on the in-
voking object.
void notify( )