Reuse of Identifiers
Permissible Reuse
public
class
SomeClass
{
There are no restrictions for using the same
identifier in two separate classes.
public int
private int
private int
fee;
fi;
foo;
There are no restrictions for using the same
identifier to name local variables and/or
parameters of different methods. (See ___
and _____.)
public void foo( int fum )
int fo;
int fee;
// code missing
}
A local variable (or parameter) can have the
same name as an instance variable of its
class. (See _____ & ____.)
public void foo( double fi )
int fo;
int fum;
// code missing
}
{
Two methods of the same class can share a }
name, so long as their parameter lists differ.
(See _____.) This is called overloading.
An instance variable and method can share
the same name within the same class. (See
_____.)
© 2006 Pearson Addison-Wesley. All rights reserved
6.3.1
{
this
Problem:
Suppose that an identifier names both an instance and local variable.
Can the instance variable be accessed within the local scope?
public
class
public
SomeClass
{
int conundrum;
public void foo() {
int conundrum;
// how to output the conundrum instance var?
}
}
Java provides a reserved word this that always refers to the current object.
This permits many things, including access to reused instance variables.
System.out.println(
.conundrum );
© 2006 Pearson Addison-Wesley. All rights reserved
6.3.2
Reuse of Identifiers
Not Permitted
Name reuse for two or more instance variables declared in the same class.
Name reuse for two or more local variables declared in the same method.
Name reuse for a local variable and parameter of the same method.
Name reuse for two methods of the same class with same parameters.
Common Sense
Don’t purposely reuse identifiers within a class, unless there is a good reason to do so.
Don’t go out of your way to avoid reusing an identifier.
© 2006 Pearson Addison-Wesley. All rights reserved
6.3.3
© Copyright 2026 Paperzz