Question: What is inner class in Java?

Ads

 
 
 

Share on Google+Share on Google+

Deepak Kumar
What is inner class in Java?
2 Answer(s)      6 years and 7 months ago
Posted in : Java Beginners
Hi,

Explain me inner classes in Java.

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

Thanks
Ads
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


Ads









Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads