A normal inner class / non static inner class

//A normal inner class / non static inner class
  1. Definition

  2. main() method in inner classes

  3. Accessing inner class code from Outer class static area

  4. Accessing inner class code from instance area of Outer class

  5. Accessing inner class code from out side of Outer class

  6. Accessing Outer class members from inside of inner class

  7. Using this keyword inside inner class

  8. Allowed Modifiers for Inner classes

1. Definition :

The Java programming language allows you to define a named no static class within another class. Such a class is called nested class or non static inner class.

Example :
//outer class
class Outer {
//inner class
class Inner {
}
}
  when inner classes compiled, for every inner class a separate java class file will be generated as below.
  Generated class file for Outer.java:   Outer.class
  Generated class file for Inner.java:   Outer$Innner.class

2. main() method in inner classes :

Inside inner class we can’t declare static members. So that it is not possible to declare main() method inside non static inner class. But static inner classes allowed to write static members, that will discuss in Static nested inner classes area. for the below program you will get compile time error.

3. Accessing inner class code from Outer class static area :

4. Accessing inner class code from instance area of Outer class :

5. Accessing inner class code from out side of Outer class :

6. Accessing Outer class members from inside of inner class :

From inner class we can access all members of outer class (both static and non-static, private and non private methods and variables) directly.

7. Using this keyword inside inner class :

Within the inner class “this” always refers current inner class object. To refer current outer class object we have to use “outer class name.this”.

8. Allowed modifiers for inner classes :

The applicable modifiers for outer classes are public, default, final, abstract, strictfp. But for Inner classes in addition to the above modifiers private, protected, static also valid.