Java Reference
In-Depth Information
TABLE 6 . 1 :
Access matrix for
static
/
instance
for methods.
(
Has access to
)
(
Static method
)
Instance method
)
Static variables and methods
YES
YES
Instance variables and methods
NO
YES
System. out . println (Die . getRandomDieValue () ) ;
Good software practices call for static method to be invoked on classes and not
objects. Invoking a static method on an object gives the false illusion to the human
reader of the code that the method is instance.
The new version of the
Die
class demonstrates
data encapsulation
. This means that the
data is only accessible from within the class in which it is defined and therefore we do
not need to worry that someone from outside the class can assign an incorrect value to an
instance variable. Note that the reason that the variable
dieValue
cannot be accessed from
outside the class is because it is defined as a
private
variable. If the variable
dieValue
were defined as a
public
variable, then the following code will be valid.
Die d =
new
Die () ;
d. dieValue =
−
4;
However, this code allows direct access to the instance variables of the class and therefore
makesthemethodsinthe
Die
class obsolete. This is the reason why all variables will be
declared as
private
throughout the topic. The only exception is constants (i.e.,
static
final
variables). They can be declared as
public
because nobody can change their value.
The
getRandomDieValue
method is defined as
private
becausewedesirethatitonly
be called from within the
Die
class. In general, methods that are part of the interface of the
class (i.e., can be called by the outside world) will be defined as
public
. Utility methods that
are only needed within the class should be defined as
private
.The
getRandomDieValue
method is defined as
static
because it is not related to a particular instance of the class
and does not access the instance variable
dieValue
.
Data abstraction
is another important feature of the object-oriented paradigm. Data
abstraction means that the details of how the data is physically stored are abstracted. For
example, someone may decide to store the variable
dieValue
in a
byte
instead of an
int
.
However, the interface of the class (i.e., its public methods) does not need to change and
therefore all the methods that interact with the
Die
class will remain the same. In other
words, we can make changes to the class that will not affect how the outside world interacts
with the class. Similarly, changes to the outside world will not affect the behavior of the
class.
We can think of a class as being its own silo that contains
private
variables and
public
interface to these variables. The code inside a class should be self-contained.
Changing it should not affect code outside the class. Similarly, changing code outside
the class should not affect the code in the class. Unless there is a good reason not to
do so, all the variables inside a class should be defined as
private
.
Consider Table 6.1. It is a matrix that tells us which methods have access to which data
and methods inside a class. For example, a static method has no direct access to instance
methods and variables because there is no current instance of the class. For example, since