What to practice
- Trace static counters and ID generators across multiple object instantiations.
- Distinguish valid vs. invalid member access inside static and instance methods.
- Predict exact caller state after passing primitive values, Strings, arrays, and objects to methods.
Current CED topics: 1.8, 1.9, 1.10, 3.6, 3.7, 3.8
Current learning objectives: 1.8.A, 1.9.A, 1.10.A, 3.6.A, 3.7.A, 3.8.A
1. Core Concept Instruction
A static variable belongs to the class itself rather than to any individual instance; all objects of the class share a single copy of a static variable (often used to count how many objects have been constructed or assign unique sequential IDs). A static method can be invoked on the class name and can only directly access static variables and other static methods—it cannot reference this or instance variables. Java ALWAYS passes arguments by value: (1) Passing a primitive copies the number/boolean, so changing the formal parameter inside the method never alters the caller's primitive variable. (2) Passing an object reference copies the reference value: reassigning the parameter to a new object (param = new Obj()) does NOT change the caller's variable, but calling a mutator method on param (or modifying param's array elements/fields) DOES mutate the shared heap object!
The compiler ignores comments; they explain code to programmers and never run. Java has three forms: // for one line, /* */ for a block, and /** */ for a Javadoc comment, which is used to create API documentation.
A precondition must be true just before a method runs for the method to behave as expected. The method is not expected to check it, so meeting it is the caller's job. A postcondition must be true after the method runs; it describes what is returned or the values of an object's attributes. For example, public static int lastDigit(int n) { return n % 10; } might be documented with Precondition: n >= 0 and Postcondition: returns the ones digit of n. The call lastDigit(47) returns 7. The call lastDigit(-47) breaks the precondition: the method does not check, and it returns -7. A least restrictive sufficient precondition is enough for the code to work and rules out no input the code handles correctly.
2. Worked Examples
Worked Pass-by-Value vs. Object State Mutation Trace
Suppose Box has field val and mutator setVal(int v). Trace x, b1.getVal(), and b2.getVal() after calling mystery(x, b1, b2) where x = 10, b1 = new Box(20), b2 = new Box(30), and:
public static void mystery(int a, Box p, Box q) {
a = 99;
p.setVal(88);
q = new Box(77);
q.setVal(66);
}
- Step 1 (Primitive Parameter a): Parameter a receives a copy of x (10). Setting a = 99 modifies only the local parameter; caller's x remains 10.
- Step 2 (Mutating Object via Reference p): Parameter p aliases the same Box object as b1. Calling p.setVal(88) mutates that shared heap object, so b1.getVal() becomes 88.
- Step 3 (Reassigning Reference Parameter q): Parameter q initially aliases b2's Box(30), but q = new Box(77) points local variable q to a brand-new Box on the heap without changing where caller's b2 points.
- Step 4 (Final State in Caller): After mystery returns, x == 10, b1.getVal() == 88, and b2.getVal() == 30.
Result:
Object Reference Aliasing vs. Primitive Value Copying Across Method Calls
Trace the contents of arr[0] and num after calling modify(arr, num):
int[] arr = {10, 20};
int num = 5;
// where modify(int[] a, int x) executes: a[0] += x; x = 99;
- Step 1 (Reference parameter mutation): Parameter a aliases arr, so a[0] += 5 mutates arr[0] in heap memory from 10 to 15.
- Step 2 (Primitive pass-by-value isolation): Reassigning local primitive parameter x = 99 does not affect caller variable num (which remains 5).
Result: arr[0] = 15 and num = 5