InnerClass in Java

package com.innertest;

/**
 *
 * @author taher_JAVAHUNTER
 */
public class OuterClass {

    public class Inner {
        public void sayGood() {
            System.out.println("Goodey");
        }
    }
    Inner inner = new Inner() {
        @Override
        public void sayGood() {
            System.out.println("Good day");
        }
        public void inMeth(){// This Method can't be called
            System.out.println("INNNN");
        }
    };

    public void show(int i) {
        class MethLocClass{
            public void ty(){
                // i=10; Local variable can't be used
                //inner = null; Instance variable will be available
            }
        }
    }

    public static void main(String[] args) {
        OuterClass oc = new OuterClass();
        oc.inner.sayGood();
        OuterClass.Inner inner = new OuterClass().new Inner();
        inner.sayGood();
        OuterClass.StatClass statClass  = new OuterClass.StatClass();
        statClass.statMeth();
    }

    public static class StatClass{ // It is a nested class not an InnerClass
        int k=10;
        public void statMeth(){
                System.out.println("In statMeth : "+k);
        }
    }
}
Output :

Good day
Goodey
In statMeth : 10

Advertisements
This entry was posted in Core JAVA, Just 4 Knowledge, SCJP. Bookmark the permalink.

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