/** * Demo of variable scoping in Java. */ public class ScopeDemo { // instance var x: scope is the entire class private int x; public ScopeDemo(int val) { // param val: scope is this method/constructor x = val; } public int getX() { return x; } public int calcSomething() { // local val: scope is the enclosing block int val; // scope is this method block if (x > 0) { int temp = x + 3; // scope is this conditional block temp /= 2; val = x + temp; } else { val = x - 5; } return val * 2; } public static void main(String[] args) { ScopeDemo obj = new ScopeDemo(10); System.out.println(obj.calcSomething()); } }