ScopeDemo.java

Download ScopeDemo.java

 1: // Scope Demo - Shows what happens when a field and a local variable have
 2: // overlapping scope.
 3: // Written 10/2020 by Wayne Pollock, Tampa Florida USA
 4: 
 5: public class ScopeDemo {
 6:      static int num = 22;  // Demo with and without static.
 7: 
 8:     public static void main ( String[] args ) {
 9:         ScopeDemo sd = new ScopeDemo();
10:         sd.aMethod(17);
11:         System.out.println( ScopeDemo.num );
12:     }
13: 
14:      void aMethod( int x ) {
15:         int num = 17;
16:         System.out.println( this.num );  // Also show "ScopeDemo.num" and "num"
17:     }
18: }