/** * Confuse.java * Illustrates variable scope. * * Program output is: * Inside constructor, x and y are: 0 0 * Inside startUp, x and y are: 1 2 * Inside first, x and y are: 3 5 * After call to first, x and y are: 1 2 * Inside second, a and b are: 4 6 * Inside second, x and y are: 0 0 * After call to second, x and y are: 1 2 * Inside third, x and y are: 3 5 * After call to third, x and y are: 1 2 */ public class Confuse { private int x; // instance variables (a.k.a. fields) private int y; public static void main(String[] argv) { Confuse myConfuseObject; myConfuseObject = new Confuse(); myConfuseObject.startUp(); } public Confuse() { x = 0; y = 0; // initialize fields System.out.println("Inside constructor, x and y are: " + x + " " + y); } public void first(int x, int y) { x = 3; y = 5; System.out.println(" Inside first, x and y are: " + x + " " + y); } public void second(int a, int b) { a = 4; b = 6; System.out.println(" Inside second, a and b are: " + a + " " + b); System.out.println(" Inside second, x and y are: " + x + " " + y); } public void third(int s, int t) { s = 3; t = 5; setXY( s, t); System.out.println(" Inside third, x and y are: " + x + " " + y); } public void startUp() { int x = 1, y = 2; System.out.println("Inside startUp, x and y are: " + x + " " + y); first( x, y); System.out.println("After call to first, x and y are: " + x + " " + y); second( x, y); System.out.println("After call to second, x and y are: " + x + " " + y); third( x, y); System.out.println("After call to third, x and y are: " + x + " " + y); } public void setXY( int x, int y) { this.x = x; this.y = y; } }