Nested Classes – Java

A class which is defined inside a class is called nested class.

download

/**
 * Created by Dasun De Silva on 7/18/2017.
 */
public class Outer {
    class Nested{

    }
}

Nested classes are divided into two class types such as
• Static nested classes
Static nested classes do not have the access to other members of its enclosing class.
• Non static nested classes (Inner classes)
Inner classes have accessibility to all the members of its enclosing class.

/**
 * Created by Dasun De Silva on 7/18/2017.
 */
public class Outer {
    
    class Inner{
        
    }
    static class Nested{
    
    }
    
}

Nested classes are using when you need to group classes which are using only at one place and it increases encapsulation as well. That means nested class implementation will not be open to external world.

Static Nested Classes

Static nested class cannot directly refer to its enclosing class’s instant variables or methods. But can use those object references. Look at the below example.

import java.util.*;

/**
 * Created by Dasun De Silva on 7/19/2017.
 */
public class ReferEncloseClassVariable {
    private static List list = new ArrayList();
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public static class B {
        List updList(ArrayList arrayList) {
            ReferEncloseClassVariable a = new ReferEncloseClassVariable();
            List tempStringList = a.getList();
            for (Object x : arrayList) {
                tempStringList.add(x.toString());
            }
            a.setList(tempStringList);
            return a.getList();
        }
    }
    
}

And if you need to call static nested class from external world this is how to do it. According to your project or package you have to import relevant package.

import java.util.ArrayList;

/**
 * Created by Dasun De Silva on 7/19/2017.
 */
public class Main {
    public static void main(String[] args) {
        ReferEncloseClassVariable r = new ReferEncloseClassVariable();
        System.out.println("========================PARENT CLASS INSTANTIATION");
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");

        System.out.println("========================UPDATE LIST FROM ENCLOSING CLASS OBJECT REFERENCE.");
        ArrayList z = new ArrayList(2);
        z.add("Col");
        z.add("SL");
        r.setList(z);
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");

        System.out.println("========================UPDATE LIST OF ENCLOSING CLASS FROM STATIC NESTED CLASS");
        ReferEncloseClassVariable.B b = new ReferEncloseClassVariable.B();
        ArrayList x = new ArrayList(2);
        x.add("Dasun");
        x.add("De Silva");
        System.out.println(b.updList(x));
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");
        
    }
}

Non static nested class / Inner class

Not like static nested class, inner classes can directly access to its enclosing class variables and methods. No need of object references like static nested classes. You can refer inner classes as below.

ReferEncloseClassVariable.C c = new ReferEncloseClassVariable().new C();

Check the full code below.

import java.util.*;

/**
 * Created by Dasun De Silva on 7/19/2017.
 */
public class ReferEncloseClassVariable {
    private static List list = new ArrayList();
    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public static class B {
        List updList(ArrayList arrayList) {
            ReferEncloseClassVariable a = new ReferEncloseClassVariable();
            List tempStringList = a.getList();
            for (Object x : arrayList) {
                tempStringList.add(x.toString());
            }
            a.setList(tempStringList);
            return a.getList();
        }
    }

    public class C {
        List updateList(ArrayList arrayList) {
            for (Object x : arrayList) {
                list.add(x.toString());
            }
            return list;
        }
    }
}

How to use above source code from external world as below.

import java.util.ArrayList;

/**
 * Created by Dasun De Silva on 7/19/2017.
 */
public class Main {
    public static void main(String[] args) {
        ReferEncloseClassVariable r = new ReferEncloseClassVariable();
        System.out.println("========================PARENT CLASS INSTANTIATION");
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");

        System.out.println("========================UPDATE LIST FROM ENCLOSING CLASS OBJECT REFERENCE.");
        ArrayList z = new ArrayList(2);
        z.add("Col");
        z.add("SL");
        r.setList(z);
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");

        System.out.println("========================UPDATE LIST OF ENCLOSING CLASS FROM STATIC NESTED CLASS");
        ReferEncloseClassVariable.B b = new ReferEncloseClassVariable.B();
        ArrayList x = new ArrayList(2);
        x.add("Dasun");
        x.add("De Silva");
        System.out.println(b.updList(x));
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");
        System.out.println("========================UPDATE LIST OF ENCLOSING CLASS FROM INNER CLASS");
        ReferEncloseClassVariable.C c = new ReferEncloseClassVariable().new C();
        ArrayList y = new ArrayList(2);
        y.add("Dasun 2");
        y.add("De Silva 2");
        c.updateList(y);
        System.out.println(r.getList() + " - " + r.getList().size() + " Number of elements ");
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++END");
    }
}


There are several types of inner classes and those are explained in this post..

Shadowing of static nested class

If enclosing class and nested class are having similar name variables then declaration shadows effect to variables. You cannot use a shadowed declaration with variable name alone. Check the below example.

/**
 * Created by Dasun De Silva on 7/19/2017.
 */
public class Shadow {
    public int x = 0;
    public class Mirror{
        public int x = 10;
        void displayX(){
            System.out.println("Which X ? " + x);
            System.out.println("Mirror X = " + this.x);
            System.out.println("Shadow X = " + Shadow.this.x);
        }
    }

    public static void main(String[] args) {
        Shadow.Mirror mirror = new Shadow().new Mirror();
        mirror.displayX();
    }
}

Reference URL s.
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s