// Tyler Rigsby, CSE 142 // Several examples of variables and how their declaration // location affects their scope; that is, where can they be // accessed. What lines cause compiler errors? public class ScopeExamples { static int globalVariable = 0; // global variable -- BAD STYLE. ALWAYS. public static void main(String[] args) { int mainVariable = 0; int myVar = 0; method(); System.out.println("2. Back in main:"); System.out.println(" mainVariable is now: " + mainVariable); System.out.println(" myVar is now: " + myVar); System.out.println(" globalVariable is now: " + globalVariable); } public static void method() { int myVar = 0; for (int i = 1; i <= 10; i++) { globalVariable++; myVar++; mainVariable++; } System.out.println("1. In method:"); System.out.println(" i is now: " + i); System.out.println(" myVar is now: " + myVar); System.out.println(" globalVariable is now: " + globalVariable); } }