What is inner class in Java?

What is inner class in Java?

Hi,

Explain me inner classes in Java.

What is the use of inner classes? Give examples of inner classes in Java.

Thanks
View Answers

October 5, 2010 at 4:01 PM

Hi Friend,

Please visit the following link:

http://www.roseindia.net/java/master-java/inner.shtml

Uses:

Non-static inner class keep the reference of outer class and allow to access member variable of outer class.

It helps in defining an interface of a class

Thanks

January 4, 2013 at 10:25 AM

19) inner class :
*sun microsystem release the inner classes in java 1.1 version.
a class within another class is called inner class.
it is possible to apply the public ,default ,abstract, final ,strictfp,private,protected and static modifier on inner class.


        * whenever we apply public on inner class ,it is possible to access outside.
        * whenever we apply protected on inner class, it is only possiblet ot access inside a package.It is not possible to access
          outside of the package.It is  critical to creating the subclass to inner class at outside of the package.
        * default inner class work only that package.
        * private innner class work only that outer class.

        * abstract innner class has contatin abstract methods.But the abstract methods are implementes only within another subclass of 
          same class.
        * it is not possible to create sub class to final inner class.But final and abstract are illegal combination.
        * whenever we declare the strictfp on inner class, that inner class follows IEEE 754 standart for floating point calculations.
1)regular inner class:
    This regular inner class does not contatin static members.But it is possible to access the static members and instance members of the outer class.

    ex: class Outer {
        int a=10;
        static int b=20;

        class Inner {
            int a=30;
            /* static String name="basha"; compile time error(ce): because regular inner class does not contain static members */

            public void show() {
                int a=40;
                System.out.println(a);
                System.out.println(this.a);
                System.out.println(Outer.this.a);

                System.out.println(b);
            }
        }//Inner

        public static void main(String args[]) {
            Outer o=new Outer();
            Outer.Inner i=o.new Inner();
            i.show();
        }
        }//Outer

Q) what happen whenever we compile the inner class ?

    whenever we compile the inner class, the compiler generate the inner class like

     class Outer$Inner extends java.lang.Object {
        final Outer this$0;
        Outer$Inner(Outer o) {
            this$0=o;
        }
        public void show();
     }


    2)method local inner class :
        * declaring the inner class inside the method is called the method inner class.
        * the method inner class can access the static and instance variables of outer class.
        * the method inner class access the only final local variables of the method.
        * the method inner class does not contain any static declarations.
        * the scope of the method inner class is only that method ,Hence we create the object inside the method after declaring
          the method inner class.



        ex:
            class Outer {

                int a=10;
                static int b=20;

                public void show() {
                    final int c=30;
                    int d=40;

                    class MInner {
                        static int e=50 ;// ce: method local inner class does not contain static members.
                        public void iShow() {
                            System.out.println(a);
                            System.out.println(b);
                            System.out.println(c);
                            System.out.println(d); //ce: because d is not a constant.

                        }   
                    }//MInner

                    MInner mi=new MInner();
                    mi.ishow();
                }//show()

                public static void main(String args[]) {
                    Outer o=new Outer();
                    o.show();
                }
            }//Outer

    3)annoymous inner class:

        if a inner class does not have any name than that inner class is called annoymous inner class.
        the annoymous inner class is sub class to parent class but it does not have any name.
        hear annoymous reference values is handled by the super class reference variable.

        a)declared by extending the a class

            class Parent { }

            class Demo {
                public static void main(String args[]) {
                    //this code is creating the annoymous class that is sub class to Parent class and handling the
                    // reference value by Parent class reference variable

                    Parent p=new Parent() {
                        void show() {
                            System.out.println("annoymous inner classs show()");
                        }
                    }
                }
            }

        b) declared by implementing interface:

            class Demo {
                public static void main(String args[]) {
                    Runnable r=new Runnable() {
                        public void run() {
                            System.out.println("run method is called");
                        }
                    }
                    Thread t=new Thread(r);
                    t.start();

                }//main
            }//Demo             

        c)declared as method argument:

            class Demo {
                public static void main(String args[]) {
                    Thread t=new Thread(new Runnable() {
                            public void run() {
                                System.out.println("run method is executed");
                            }   
                        });
                     t.start();
                }
            }

    4)static inner class:
        * declaring the inner class by using static modifier.
        * the static inner class has contain static members and also instance member.
        * but static inner class does not access the instatnce variables of outer class.
        * this static inner class not really inner class ,because it is executed without depends on outer class.

        class Outer {

            int a=2;
            static int b=3;

            static class Inner {
                public static void main(String args[]) {
                    //System.out.println(a);//ce
                    System.out.println(b);
                }
            }//Inner
        }//Outer

                               compile: javac Outer.java
                               run :      java Outer$Inner

where we use:

Generally we are using only annonymaus innerclass only as method argument in swings and threads and spring framework









Related Tutorials/Questions & Answers:
What is inner class in Java? - Java Beginners
inner class
Advertisements
difference between java5 and java6 - Java Beginners
What is the difference between a static and a non-static inner class?
Inner class
Java inner class
Inner class
Inner class
about inner class
Inner Class - Java Beginners
Java2
java inner class - Java Beginners
Java inner class and static nested class
ModuleNotFoundError: No module named 'inner-class'
Inner class in java
Javah
inner class - Java Interview Questions
Inner class - Java Interview Questions
Use of Local Inner class
Inner class in java
new operator for creating the object of inner class
about java1
What is an Abstract Class and what is it's purpose?
Javap Tool application
About Java2
What is Action Class?
What is an abstract class?
What is final class?
What is Calendar class in Java?
What is the Locale class?
what is the size of java class
Javah -  Header File Generator
javaa swings - IDE Questions
what is class methods in objective c
what is class methods in objective c
what's the purpose of constructor in abstract class?
What is a local, member and a class variable?
What is the base class of all classes?
javas - JSP-Servlet
javab - Java Beginners
What is nested class in Java with Example?
What is nested class in Java with Example?
What are the difference between abstract class and interface?
What does it mean that a class or member is final?
What are the different types of property and class mappings?
If I open .class file with notepad what is displayed?
Javac -source KeyTest.java
Nested and Inner classes
Artifacts of javax
What happens to the static fields of a class during serialization?

Ads