Java Inner Classes

By ongraph
January 13, 2015 | 869 Views

Latest Industry insights to keep you updated on the latest happenings.

What is Inner Class in Java ?

One class within another class is known as Inner Class.Just as classes have member variables and methods, a class can also have member classes. This table shows the types of nested classes:

Types of Nested Classes
Type Scope Inner
static nested class member no
Inner [non-static] class member yes
Method-local class local yes
Anonymous class only the point where it is defined yes

 

 

 

 

 

 

 

Type:-1 (Static Nested Class)

A static nested class is not an inner class, 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.

Syntax :-

 public class OuterClass {
static InnerClass  {
void printData() {
System.out.println(“Static Nested Inner Class”);
}
}
}
 Here we can access InnerClass members directly.
InnerClass ic = new InnerClass();
ic.printData();
//Output : Static Nested Inner Class

 

Type:-2 (Inner [non-static] Class)

Inner class instance has access to all members of the outer class, even those marked private.
The only way you can access the inner class is through a live instance of the outer class!

class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner(); // make an inner instance
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println(“Outer x is ” + x);
}
}
}
Access inner class outside outer class
public static void main(String[] args) {
MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
}

 

Type:-3 (Method-local Inner Class)

A method-local inner class can be instantiated only within the method where the inner class is defined.

class MyOuter2 {
private String x = “Outer2”;
void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println(“Outer x is ” + x);
} // close inner class method
} // close inner class definition
MyInner mi = new MyInner(); // This line must come
// after the class
mi.seeOuter();
} // close outer class method doStuff()
} // close outer class

Note :- If we declare any local variable in doStuff() method,then it’s need to be marked final,due to it’s access level.

 

Type:-4 (Anonymous Inner Class)

Inner classes declared without any class name is known as Anonymous inner class.you can define these classes not just within a method, but even within an argument to a method.

class Popcorn {
public void pop() {
System.out.println(“popcorn”);
}
}
class Food {
Popcorn p = new Popcorn() {
public void pop() {
System.out.println(“anonymous popcorn”);
}
};
}

Here the Popcorn reference variable refers not to an instance of Popcorn, but to an instance of an anonymous (unnamed) subclass of Popcorn. We can see here Anonymous popcorn class override superclass pop method.

This is the whole point of making an anonymous inner class—to override one or more methods of the superclass!

Advantage of Inner Class :-
The inner class shares a special relationship with the outer class i.e. the inner class has access to all members of the outer class. This increases the level of encapsulation.

Disadvantage of Inner Class :-
Using inner class increases the total number of classes being used by the application. For all the classes created by JVM and loaded in the memory, jvm has to perform some tasks like creating the object of type class.

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin