Nested blocks and variable scopes - Java Language Basics

Java examples for Language Basics:Variable

Description

Nested blocks and variable scopes

Demo Code

public class Main {
    public static void main(String[] arg) {

        boolean x = false;

        if (x = true) System.out.println("x is true");
        else System.out.println("x is false");

        int i = 3;
        int j = 4;

        System.out.println("i=" + i + " j=" + j);

        {//from   w  w w .  ja v  a  2s. c o m
            // Cannot redefine a variable i here
            int ii = 5;
            j++;
            i--;
        }

        System.out.println("i=" + i + " j=" + j);
        // Cannot access variable ii here           
    }
}

Result


Related Tutorials