ITEC 120: Week 12
- Last day to withdraw with a W: Friday of this week
- Exam on Monday!
- Program 4 due at noon on Friday of next week (before break)!
- Notes:
- The
this
reference
- Within a class,
this
refers to the invoking object
- When a method is called, this is a copy of the value of the invoking
reference (eg when
b.getArea()
is called, the this
reference gets a copy of
the value of b
)
- Motivation 1: consistency of access (eg
b.getArea();
this.getArea();
)
- Motivation 2: makes it possible to highlight the fact that a field in
the current object is being accessed (eg
return this.length *
this.width;
)
or a method is being invoked on the
current object (eg return this.getLength() * this.getWidth();
)
- Motivation 3: Allows access to a field that is hidden by a parameter
with the same name
- Motivation 4: Allows passing the invoking object as a parameter (eg
myX.foo(this);
). We will see examples of this later.
- More on Parameters
- Formal and actual parameters
- Formal parameter: defined in the method declaration
- Actual parameter: used in the method call
- Actions that occur on method call:
- Formal parameters are allocated
- The value of each actual parameter is copied into the
corresponding formal parameter
- When the method uses a parameter, it uses the copy, not the original
- Effects of changes to formal parameters
- Changes to primitive values are NEVER reflected in the actual
parameter
- Reference parameters can change the fields of the object pointed to
- But reference parameters in the caller can NEVER be made to point to
another object
- Overloading
- When methods in a class have the same name and different signatures
- Method signature: parameter number, type, and order
- Signature does not include the method's return type and the parameter
names
- Example: Versions of method
println
are defined for ints,
Strings, chars, ...
- Lab 19
- Lab 20
- Examples from Chapter 6
- Chapter 6 Powerpoint Slides
- Semester
Calendar