Inner Classes in Java

Inner class is a class within another class, method or block. An inner class can be one of the following four types: Anonymous, Local, Member and Nested top-level. 

An anonymous inner class is an inner class without a name and is created as part of a statement.

A inner class declared within a method is called method-local inner class.

A member inner class is a regular inner class declared within a class, but outside all methods.

Static inner classes or nested top-level inner classes are actually nested classes that are inner classes marked with the static modifier.


A regular or member inner class

A "regular" inner class inside the class, but outside all methods or blocks, is a normal member of the enclosing (outer) class, and can be marked with any access modifier and/or, an abstract or a final modifier. Note that you can't use both abstract and final together for an inner class or even a top-level class.

A regular non-static member inner class instance has access to all of the outer class's members, including those marked private.

To instantiate an inner class from outside the enclosing class (outer class), you must have a reference to an instance of the outer class. 

MyOuter mo = new MyOuter(); //instance of outer class

MyOuter.MyInner inner = MyInner();

To access an inner class, you have to use the outer class name with dot operator before it as: OuterClass.InnerClass.

From code within the enclosing class (outer class), you can instantiate the inner class using only the name of the inner class, just like a regular class. 

From code within the inner class, the keyword 'this' holds a reference to the inner class instance.

To reference the enclosing class (outer class) 'this' from an inner class, precede the keyword 'this' with the outer class name as:



Method-Local Inner Classes

A method-local inner class is defined within a method of the enclosing class.  

You must instantiate this inner class within the same method, but after the class definition code.

A method-local inner class can only use final variables within the method.

The only modifiers you can apply to a method-local inner class are abstract or final, but never both at the same time.

We can declare a method-local inner class with the same name as a regular inner class within the same class and when accessed from within the local block (the method), local inner class will take precedence, just like a local variable take precedence over an instance variable.  


Anonymous Inner Classes

Anonymous inner classes have no name, and they either subclass a class or implement an interface.

An anonymous inner class is always created as part of a statement and hence might have to end with a semicolon when applicable.

An argument-defined inner class is declared, defined, and automatically instantiated as part of a method invocation and should not end with a semicolon.

We can define an anonymous inner class for an interface as new InterfaceName(){ //implementation code } and use it in places where an object of type InterfaceName is required.

interface Foo { int bar(); }

public class Sprite {

public int fubar( Foo foo ) { return; }

public void testFoo() {

fubar( new Foo() { public int bar() { return 1; });



We are not instantiating an Interface here. We are actually creating an object of a new, anonymous inner class that implements interface Foo using a special syntax.

Because of polymorphism, the only methods you can call on an anonymous inner class reference are those defined in the reference variable class (or interface).

Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot both extend a class and implement an interface, nor can it implement more than one interface.


Static Nested Classes

Static nested classes are inner classes marked with the static modifier.

A static nested class is not like a regular inner class, but it's a top-level nested class.  

Because the nested class is static, it does not share any special relationship with an instance of the outer class. In fact, you don't need an instance of the outer class to instantiate a static nested class.

Instantiating a static nested class requires using both the outer and nested class names as follows:

OuterClass.StaticInner n = new OuterClass.StaticInner();


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)