Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Nested and Inner Class

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 248
    Comment on it

    Nested Class:

    A class written inside another class is known as nested class, and the class that contains the inner class is called the outer class.

    Nested class are of two types Static class and  Non-static inner class. And non-static nested class are known as Inner class.

    Syntax of Nested class:

        class OuterClass{  
         //code .... 
         class InnerClass{  
          //code ....  
         }  
        }  

    Inner Class:

    A class that is written inside a class but outside a method is known as inner class. Inner class can be private but it cannot be accessed from an object outside that class.

    Example:

    class OuterClass{
       int num;
       //inner class
       private class InnerClass{{
          public void print(){	   
          System.out.println("This is an inner class");
          }
       }
       //Accessing he inner class from the method within
       void displayInner(){
          InnerClass inner = new InnerClass();
          inner.print();
       }
    }
       
    public class Iclass{
       public static void main(String args[]){
          //Instantiating the outer class 
          OuterClass out=new OuterClass();
          //Accessing the displayInner() method.
          out.displayInner();
       }
    
    }

    It will print output :

    This is an inner class

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: