------------------------------------------------------------------------------
MC logo
Java Class Scope
[^] Names and Scope
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Java Class Scope]
fred.java
import java.io.*;
import java.util.*;

public class fred {
    static class Outer {
        // This variable exists in the outer scope
        private int ferdie;
        private class Inner {
            // This variable exists in the inner scope
            private int dingle;

            public Inner(int d) 
            {
                dingle = d;
            }
            public void report()
            {
                // The reference to the outer scope and then the inner.
                ferdie += 10;
                dingle += 10;
                System.out.println("Report: " + ferdie + " " + dingle);
            }
        }
        private Inner g1, g2, g3;
        public Outer() 
        {
            ferdie = 10;
            g1 = new Inner(100);
            g2 = new Inner(200);
            g3 = new Inner(300);
        }
        public void report()
        {
            System.out.println("Report: " + ferdie);
            g1.report();
            g2.report();
            g3.report();
            System.out.println("Report: " + ferdie);
        }

    }

    public static void main(String[] args)
    {
        Outer z = new Outer();
        z.report();
    }
}
// Note: This program can be translated to C++ only by passing each Inner object
// a pointer to the Outer object.