Chapter 5
Section 5.1
- declaration of
reference variable and object it refers to are two separate steps
- reference variable
holds address of an object
- use dot operator to
invoke object’s method – actually using address of reference variable to
locate object in memory
- reference variable that
doesn’t point to an object has null reference
- if tried to follow that
reference, get NullPointerException thrown
- can initialize objects
to null so we know they don’t point to anything
- can assign two
references to refer to the same object – p. 223, figure 5.1
- this makes them aliases
of each other
- now when use one
reference to change the state of that object, it is changed for other
reference too
- every object has equals
method defined for it, and works like = = unless we define it differently
- it is invoked through
one object with the other as a parameter
- returns true or false
depending on if the objects are aliases of each other or not
- for Strings, = = and
equals work differently
- when all references of
an object are lost, that object can no longer be used and is considered
garbage
- Java has automatic
garbage collection
- can define finalize
method to be called on garbage collection
- Java passes all
parameters by value – current value of actual paramter is copied into
formal parameter of method header
- so formal parameter is
a copy of the value passed in, so changes made to it in the method have no
effect on the actual parameter
- when an object is
passed to a method, a reference to that object is what is really passed,
so the formal and actual parameters become aliases of each other
- listing 5.1, 5.2, p.
226, 228
- this reference used to
allow an object to refer to itself
- refers to object
through which a method was invoked, for example
Section 5.2
- static variables are
shared among all instances of a class – only one copy exists for all
objects of a class
- changing value of a
static variable changes it for all objects
- memory is established
class containing it is referenced the first time
- constants are declared
static many times since they don’t change anyway
- methods can also be
static – these can be invoked through the class itself without
instantiating an object first
- main must be declared
static so the interpreter can execute it without instantiating the class
that contains the main method first
- all methods of Math
class are static
- static methods can’t
reference instance variables, but can reference static variables
- listing 5.4, 5.5, p.
233, 234
Section 5.3
- can declare a class
within another class – nested classes
- nested class is
considered member of enclosing class
- nested class has access
to enclosing class’s variables and methods, even private ones
- produces its own
bytecode file, with name being enclosing class name $ nested class
name.class
- nested class can be
static
- nonstatic nested class
is called inner class
Section 5.4
- interface is collection
of constants and abstract methods, methods with no implementation
- an interface can’t be
instantiated
- a class implements an
interface by providing method implementations for all the abstract methods
of the interface
- use implements
- that class must provide
definitions for all abstract methods
- listing 5.6, 5.7, p.
236, 237
- a class can implement
more than one interface, listing them with commas between them
- interface can have
constants, so any classes that implement them will gain access to all
constants
- can use interface name to declare a
reference variable – refers to any object of any class that implements
that interface
- listing 5.9, p240
- an object of one class
can’t be assigned a reference of another class, but interfaces give more
freedom
- it is type of object,
not type of reference, that determines which method to invoke
- polymorphism – having many forms
- polymorphic reference
refers to different types of objects at different times
- binding to methods is
not done until runtime – called late binding or dynamic binding
- can use interface name
as type of parameter to a method
- comparable interface
- iterator interface
Section 5.5
- Java has classes that
represent events we want to capture
- mouse and mouse motion
events
- listener interface –
object waiting for an event to occur, then responds in some way when it
does
- create a listener
object, then add listener to graphical component that would generate event
- create class that
implements listener interface
- when event occurs,
appropriate method of listener is called automatically, where event object
is parameter passed to method
- listing 5.10, 5.11 p.
246, 248
- listing 5.12, p. 249
- listing 5.13, p. 253
Section 5.6
- animation – image that
gives appearance of movement
- use timer object from
Timer class to create necessary pause in drawing
- timer generates action
events at regular intervals
- use action listener to
catch and handle action events
- to do animation, set up
timer, then update graphics in action listener
- listing 5.14, p. 258