<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">public class aliases_methods2  {
    public static void main(String args[]) {

	Data a1 = new Data(1);
	Data a2 = new Data(2);
	Data a3 = new Data(3);

	System.out.println("BEFORE METHOD CALL");
	System.out.println("test_a1: " + a1.k);
	System.out.println("test_a2: " + a2.k);
	System.out.println("test_a3: " + a3.k);

	test(a1,a2,a3);

	System.out.println("AFTER METHOD CALL");
	System.out.println("test_a1: " + a1.k);
	System.out.println("test_a2: " + a2.k);
	System.out.println("test_a3: " + a3.k);

    }

    public static void test(Data a1, Data a2, Data a3) {
	// make a2 an alias for a1
	a2 = a1;
	a2.k = 10;
	
	// formal and actual parameters are aliases for eachother.
	// so, changing a formal parameter's fields changes the
	// fields of the actual parameter.

	// make a3 an alias for b
	Data b = new Data(4);
	a3 = b;
	b.k = 11;

	// here, the formal parameter becomes an alias to a LOCAL
	// reference variable. so, changes to b and a3 are LOCAL changes
	// because b and its object will die when the method ends.
	// The actual parameter to still refers to the original object
	// instantiated in main(___)

	System.out.println("DURING METHOD CALL");
	System.out.println("test_a1: " + a1.k);
	System.out.println("test_a2: " + a2.k);
	System.out.println("test_a3: " + a3.k);

    }

}

class Data {
    int k;
    Data(int k) {
	this.k = k;
    }
}

/* Output:

BEFORE METHOD CALL
test_a1: 1
test_a2: 2
test_a3: 3
DURING METHOD CALL
test_a1: 10
test_a2: 10
test_a3: 11
AFTER METHOD CALL
test_a1: 10
test_a2: 2
test_a3: 3

*/
</pre></body></html>