Blog Image

Java Inner Classes  

In This blog i am explaining about the java inner classes .What is java inner classes,different types of inner classes and how we can declare a anonymous inner classes with simple example i am explaining now.

Inner Class:
Declaring the class inside another class such type of classes are called as inner classes.
Inner classes logically group classes and interfaces in one place only and it is increase the use of encapsulation, and create more readable and maintainable code.
Based on the position inner classes are divided into 4 types.
1. Normal Inner classes
2. Method Local inner classes
3. Anonymous inner classes
4. Static nested classes.
Normal Inner classes:
Java inner class is defined inside another class directly with out static modifier,such type of inner classes are called as normal inner classes.
Java inner classes is associated with the object of outer class and it can be access all the variables and methods of outer class.
The object of java inner class are the part of the outer class object and to create a instance of inner class,first we need to create the instance of outer class.


class JavaOuter
{
    class JavaInner
    {
        public void innerMethod()
        {
        System.out.println("Inner class method is called");
        }
    }
    public void outerMethod()
        {
            JavaInner ji=new JavaInner();
            ji.innerMethod();
        }
    public static void main(String[] args)
        {
            JavaOuter jo=new JavaOuter();
            jo.outerMethod();
        }
}
output:Inner class method is called.

From inner class we can access all members of outer class (both static and non static, private methods and variables) directly.

package com.sjg.innerclass;

public class NormalInnerClass {
    static int x = 10;
    int y = 20;
    private static int z = 30;
    class Innerclass {
        public void innerMethod() {
            System.out.println("values of x ="+ x + " y ="+y+" z ="+z);
        }
    }
    public static void main(String[] args) {
        //NormalInnerClass jo = new NormalInnerClass();
        new NormalInnerClass().new Innerclass().innerMethod();
    }
}
output:values of x =10 y =20 z =30

Method Local inner classes:
Some times we can declare a inner class inside a method of an outer class such type of inner class are called method local inner classes.

class SjgOuterClass {
    void outerMethod() {
        System.out.println("inside outerMethod");
        class SjgInnerClass {
            void innerMethod() {
                System.out.println("inside innerMethod");
            }
        }
        SjgInnerClass si = new SjgInnerClass();
        si.innerMethod();
    }
}
class MethodDemo {
    public static void main(String[] args) {
        SjgOuterClass x = new SjgOuterClass();
        x.outerMethod();
    }
}

Method local inner class we cannot access the local variables of a method.To resolve that declare that variable as final then we will not get any compile time errors.See in below one,

package com.sjg.innerclass;
public class SJGTestInnserClass {
    int x = 100;
    public void methodOuter() {
         int y = 108;
        class Inner {
            public void methodInner() {
                System.out.println(x);
/*Cannot refer to the non-final local variable y defined in an enclosing scope
 Change modifier of 'y' to final   */
                System.out.println(y);
            }
        }
        Inner i = new Inner();
        i.methodInner();
    }
    public static void main(String[] args) {
        new SJGTestInnserClass().methodOuter();
    }
}

Anonymous inner classes:
We can declare the inner class without the name such type of inner classes are called Anonymous inner classes.
Anonymous inner class always extends from class or implement an interface.
Anonymous inner classes are accessible only at the point where it is defined.

package com.sjg.innerclass;
public class AnonymousInnerClassTest {
    public static void main(String[] args) {
        AnonymousInnerClassMain p = new AnonymousInnerClassMain() {
            public void anonymousMethod() {
                System.out.println("anonymous methos is called");
            }
        };
        p.anonymousMethod();
        AnonymousInnerClassMain p1 = new AnonymousInnerClassMain();
        p1.anonymousMethod();
    }
}
public class AnonymousInnerClassMain {
    public void anonymousMethod()
    {
    System.out.println("main method is called");
    }
}

There are 3 types of anonymous inner classes
1. Anonymous inner class that extends a class.
2. Anonymous inner class that implements an interface.
3. Anonymous inner class that defined inside method arguments.

package com.sjg.innerclass;
public class AnonymousInnerClassImplementsInterface {
    public static void main(String[] args) {
        Runnable r = new Runnable()
        {
            public void run() {
                System.out.println("This is an example of anonymous inner class implementing intreface");
            }
        };
        Thread t = new Thread(r);
        t.start();
        System.out.println("calling Main thread");
    }
}

Anonymous inner class that defined inside method arguments.

public static void main(String[] args) {
    new Thread(new Runnable() {
        public void run() {
            System.out.println("This is an example of anonymous inner class as an argument");
        }
    }).start();
    {
        System.out.println("thread is calling");
    }
}

Static nested classes:
If we can declare inner classes with static modifier such type of inner classes are called static nested classes.
In static nested classes with out existing outer class object there may be chance of existing static nested classes.

package com.sjg.innerclass;
public class StaticNestedClasses {
    private static void outerMethod() {
        System.out.println("calling outerMethod");
    }

    static class Inner {
        public static void main(String[] args) {
            System.out.println("Calling inside inner class method");
            outerMethod();
        }
    }
}


You can download the above maven example from the attachments given below.

Attachments

About author

User Image
sagarreddy

my self is SagarReddy. I from bhadrachalam and i am working StackSpace software solutions pvt. Ltd. has a java developer. Learning the new things from authors to update my self to build my career Better Way.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.