public class Demo1 { public static void main( String[] args ) { System.out.println("Mystery:"); { int x = 123; // Variable x System.out.println( x ); // (1) } System.out.println( x ); // (2) This statement will produce an error // Why ??? } } |
Experiment:
|
|
public class Demo2 { static int x = 123; // Variable x public static void main( String[] args ) { System.out.println("Mystery:"); { System.out.println( x ); // (1) } System.out.println( x ); // (2) } } |
Experiment:
|
|
When a variable can be access from only within a certain place in a program, the information stored in such variable will be short term.
(I.e., the information will not be retained when the program exection leaves the specific area in the program).
|
public class Demo3 { static int x = 123; // Variable x (1) public static void main( String[] args ) { int x = 777; // Variable x (2) { System.out.println( x ); // (1) x = ? } System.out.println( x ); // (2) x = ? } } |
Experiment:
|
public class Demo4 { static int x = 123; // Variable x (1) public static void main( String[] args ) { { int x = 777; // Variable x (2) System.out.println( x ); // (1) x = ? } System.out.println( x ); // (2) x = ? } } |
Experiment:
|