Objects, references, etc.
You may be sure that the final exam will contain a question
similar to Problem 1 of the midterm exam. The key points to keep in mind are:
- A variable in Java is a reference to an object.
- A new object is created by calling new.
- The assignment P = Q makes P and Q
references to the
same object. Therefore, changes to that object affect both
P and Q.
- Parameter passing in call by value, but the parameters are themselves
if you call f(P) and f is declared with
argument Q, then P and Q
are different variables, but are references to the same object. Changing
that object under the Q reference changes it
under the P reference.
Reassigining Q has no effect on P.
- Java uses dynamic dispatching for the owner of the method.
So if you call P.f(), then definition of f
used is determined by the
actual type of the object that
P references when the call is carried out.
- Java uses static disambiguating of overloading. So if P is of
class C which has multiple definitions of method f and you call
p.f(Q), then the choice of method depends on the
class declaration of Q.