Home > Java > Classes > Inner Classes > Introduction
Introduction

Introduction to Inner Classes

It’s possible to place a class definition within another class definition. This is called an inner class. The inner class is a valuable feature because it allows you to group classes that logically belong together and to control the visibility of one within the other. However, it’s important to understand that inner classes are distinctly different from composition.


A nested class is known only to its enclosing class. Thus, the scope of a nested class is limited to that of its outer class. A nested class has access to the members, including private members, of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.


Sometimes an inner class is used to provide a set of services that is used only by its enclosing class.


Syntax


class OuterClass {


     //field, constructor, and method declarations

 

     class InnerClass {


        //field, constructor, and method declarations


     }


}


Example


1.  /**
2.  * @(#)NestedClassDemo.java
3.  *
4.  *
5.  * @author
6.  * @version 1.00 2009/11/13
7.  */
8.
9.  // Use an inner class.
10. class Outer {
11.    
12.    int nums[];
13.    
14.    public Outer(int n[]) {
15.            nums = n;
16.    }
17.    
18.    public void Analyze() {
19.
20.        Inner inOb = new Inner();
21.        
22.        System.out.println("Minimum: " + inOb.min());
23.        System.out.println("Maximum: " + inOb.max());
24.        System.out.println("Average: " + inOb.avg());
25.        
26.      }
27.
28.    // This is an inner class.
29.    class Inner {
30.        
31.        public int min() {
32.
33.            int m = nums[0];
34.    
35.            for(int i=1; i < nums.length; i++)
36.                if(nums[i] < m) m = nums[i];
37.            
38.            return m;
39.        }
40.        
41.        public int max() {
42.            
43.            int m = nums[0];
44.        
45.            for(int i=1; i < nums.length; i++)
46.                if(nums[i] > m) m = nums[i];
47.        
48.            return m;
49.            
50.        }
51.        
52.        public int avg() {
53.        
54.            int a = 0;
55.        
56.            for(int i=0; i < nums.length; i++)
57.                a += nums[i];
58.            
59.            return a / nums.length;
60.        
61.        }
62.    } // End Inner class
63. }// End Outer class
64
65  public class NestedClassDemo {
66        
67    /**
68     * Creates a new instance of NestedClassDemo.
69     */
70    public NestedClassDemo() {
71    }
72    
73    /**
74     * @param args the command line arguments
75     */
76    public static void main(String[] args) {
77        // TODO code application logic here
78        
79        int x[] = { 3, 2, 1, 5, 6, 9, 7, 8 };
80
81        Outer outOb = new Outer(x);
82        outOb.Analyze();
83        
84    }
85 } // End NestedClassDemo class


Output


Minimum: 1
Maximum: 9
Average: 5

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel