Java-Staic Inner Class

Staic Inner Class

There are two types of nested class: static and non-static. Nested class that are defined static are called static nested class. Non-static nested class are called inner class. Nested static class doesn’t need reference of Outer class. static nested class cannot access non-static data member or method. To create an object for the static Inner class, use this syntax :

Program

1

Program Source

class Outer {
        
    class NonStaticInner {
        
        void nonStaticInnerMethod() {
            
            System.out.println("NonStaticInnerMethod Called");
        }
    }
    
    static class StaticInner {
        
        void staticInnerMethod() {
            
            System.out.println("StaticInnerMethod Called");
        }
    }
}

public class Javaapp {

    public static void main(String[] args) {

        Outer outer = new Outer();
        Outer.NonStaticInner nostin = outer.new NonStaticInner();
        nostin.nonStaticInnerMethod();
        
        Outer.StaticInner stin = new Outer.StaticInner();
        stin.staticInnerMethod();
    }
}

Leave a Comment