java inner classes tutorial

java inner classes tutorial in this topic you will learn about Types of inner classes, Member class, Nested top-level class, Local class,  Anonymous class along with syntax and examples.

This is most important topic most of the times quotations are asked from this topic.


java inner classes tutorial

Inner class:

This new feature lets you define a class part of another class. Just as fields and methods are defined within classes. Inner classes can be used to support work of the class in which they are contained  An inner class is a class that is defined inside another class.

Inner classes nest within other classes. A normal class is a direct member of a package, a top-level class. An inner class instance has the special relationship with Outer class. This special relationship gives inner class access to a member of the outer class as if they are the part of an outer class.

Inner class instance has access to all member of the outer class

(Public, Private & Protected)

Why inner class?

There are three reasons:

Inner class methods can access the data from the scope in which they defined, including that would otherwise be private

The inner classes are hidden from other classes in the same package.

They provide modularity

a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

The non-static inner class is called member class.

If the class is defined inside another class without static modifier is called member class.

Rules for member class:

it can access the members of the outer class directly (including private)

an object cannot be created outside outer class/member class cannot access outside outer class

Members of member class cannot access by directly need to create an object.

Member class can be declared as private public.protected or default.

Next Page