Академический Документы
Профессиональный Документы
Культура Документы
Ali Kassem
Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
Enhancing Classes
Now we can explore various aspects of classes and objects
in more detail
Chapter 5 focuses on:
Ali Kassem 2
References
Recall from Chapter 2 that an object reference variable
holds the memory address of an object
Rather than dealing with arbitrary addresses, we often
depict a reference graphically as a “pointer” to an object
ChessPiece bishop1 = new ChessPiece();
bishop1
Ali Kassem 3
The null Reference
An object reference variable that does not currently point
to an object is called a null reference
The reserved word null can be used to explicitly set a null
reference:
name = null;
if (name == null)
System.out.println ("Invalid");
The null Reference
An object reference variable declared at the class level (an
instance variable) is automatically initialized to null
obj2.tryMe();
The this reference
The this reference can also be used to distinguish the
parameters of a constructor from the corresponding
instance variables with the same names
num2 = num1;
Before After
5 12 5 5
Ali Kassem 8
Reference Assignment
For object references, assignment copies the memory
location:
bishop2 = bishop1;
Before After
Ali Kassem 9
Aliases
Two or more references that refer to the same object are
called aliases of each other
One object (and its data) can be accessed using different
reference variables
Aliases can be useful, but should be managed carefully
Ali Kassem 10
Testing Objects for Equality
The == operator compares object references for equality,
returning true if the references are aliases of each other
bishop1 == bishop2
bishop1.equals(bishop2)
Ali Kassem 12
Objects as Parameters
Parameters in a Java method are passed by value
class Helper
Ali Kassem 17
Static Methods
The order of the modifiers can be interchanged, but by
convention visibility modifiers come first
Recall that the main method is static; it is invoked by the
system without creating an object
Static methods cannot reference instance variables, because
instance variables don't exist until an object exists
However, a static method can reference static variables or
local variables
Ali Kassem 18
The static Modifier
Static methods and static variables often work together
For example
Enclosing Class
Nested Class
Nested Classes
A nested class has access to the variables and methods of
the enclosing class, even if they are declared private
In certain situations this makes the implementation of the
classes easier because they can share information easily
Furthermore, the nested class can be protected by the
enclosing class from external use
This is a special relationship and should be used with care
Nested Classes
A nested class produces a separate bytecode file
Outside.class
Outside$Inside.class
Nested classes can be declared as static, in which case they
cannot refer to instance variables or methods
Inner Classes
A nonstatic nested class is called an inner class
A semicolon immediately
follows each method header
Interfaces
An interface cannot be instantiated
// etc.
}
Interfaces
A class that implements an interface can implement other
methods as well
See Complexity.java (page 294)
See Question.java (page 295)
See MiniQuiz.java (page 297)
Event
Component Listener