Java Nested and Inner Class

In java programming it is possible to define a class within another class, such classes are known as nested class.

There are two types of nested classes:-                                                                                                                                1. Non-static inner class                                                                                                2.  Static inner class

Non-static inner class: Non-static inner class has a access to all of the variables and member of its outer class and can refer them directly.

Static inner class

static inner class can not access non-static member of its outer class. A static inner class can only access static member of its outer class.

Result

                This program will not compile because static inner class can not access non-static member of its outer class. But if we  will declare “x” as static then 

static int x = 100; 

the above program will run because x is a static member.

Note: In the above program, object of static inner class  created in outer class method but if we want to create object of static inner class in main() method. We can create by using syntax:

OuterClass.InnerClass  obj_name =new    OuterClass.InnerClass();