Inner-outer class in java program

What is inner-outer class?

When we define B class into A class, that time A is outer class and B is inner class.

class A            // Outer class

{

            class B            // inner class

            {

}

}

Inner class can’t access from main class. For access inner class we have to reach outer class methods then we can access inner class methods and variables from outer class method.

 

Let’s see example on inner-outer class program in java.

class outer1

{

            class inner1

            {

                        void setMethod1() // How to access this method?

                        {

                                    System.out.println(“Welcome in Inner Class”);

                        }

            }

            void setMethodOut1()

            {

                        System.out.println(“Outer Class”); // 1st

                        // We can create an object of inner class

 

                        inner1 obj = new inner1();

                        obj.setMethod1();        // call into inner class

            }

}

class clEx14

{

                        public static void main(String args[])

                        {

                                    // create an object of inner class

                                    //inner1 obj = new inner1(); // Can’t able to create inner class object

                                    //obj.setMethod1();     

 

                                    outer1 ob = new outer1();

                                    ob.setMethodOut1(); // call to outer class method

                        }

}

 

Output:

Outer Class

Welcome in inner class

 

In this blog example, we can’t able to create an object of inner class, so we have to create outer class object then call to outer class method. After that we have to create object of inner class in outer class method.

So this is most secure class structure of java programs.

 

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn Money Online

428total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image