Skip to content

Nested Classes In Java Programming Language

In this article, you will learn the use of Nested Classes in Java Programming Language.

Nested class is nothing, but just an another class created inside a class. It is also known as inner class. The class inside which an inner or nested class is created is known as the outer class.

We have shown below the syntax of the nested class used in java programming language. If you want professionals assistance then feel free to join our Java classes and get your hands dirty with live projects.

Syntax of Nested Class:

class Outer {
//code goes here
class Nested {
//code goes here
}
}

Here ‘Outer’ is the outer class and ‘Nested’ is the inner class.

Advantages of Nested Class:

  • A nested class makes a program more readable and maintainable.
  • A nested class helps to achieve better encapsulation.
  • Better code optimization.

TYPES OF NESTED CLASSES

There are two types of nested classes in java programming language.

  1. Static Nested Class
  2. Non-Static Nested Class
nested-class-in-java

[A] STATIC NESTED CLASS

A nested class having a static modifier attached to it is known as static nested class. Since, a static keyword is used, just like static methods, it cannot directly access the instance variables and methods of the outer class. It can refer or access them only through an object.

To access the static nested class, the following syntax is used.

OuterClass.StaticNestedClass

To access the instance variables and methods of the outer class, an object should be created. Following is the syntax to create an object.

OuterClass.StaticNestedClass nestedobj = new OuterClass.StaticNestedClass() ;

Syntax:

class OuterClass {
static class NestedClass {
}
}

Example:

class OuterClass {
static class NestedClass {
public void method() {
System.out.println(“Example of Static Nested Class”) ;
}
}

public static void main(String args[]) {
OuterClass.NestedClass nested_obj = new OuterClass.NestedClass() ;
nested_obj.method() ;
}
}

Output:

Example of Static Nested Class

[B] NON-STATIC NESTED CLASS

Unlike a static nested class, a non-static nested class is one which can access the members(instance variables and methods) of its outer class. It is also known as Inner Class.

In java programming language, a class cannot be declared with a private access modifier. However, a nested class or an inner class can be declared as private, thus, making it inaccessible by an object outside the class.

Depending upon the declaration of the inner class, it is divided into three parts.

  1. Member Inner Classes
  2. Local Inner Class
  3. Anonymous Inner Class
non-static-nested-in-java

[1] MEMBER INNER CLASS

A class which is created inside a class but outside the method is known as Member Inner Class.

Example:

class OuterClass {

private class MemberInnerClass {
public void print() {
System.out.println(“Example of Member Inner Class”) ;
}
}

public void printInner() {
MemberInnerClass innerobj = new MemberInnerClass() ;
innerobj.print() ;
}
}

class InnerClassExample {
public static void main(String args[]) {
OuterClass outerobj = new OuterClass() ;
outerobj.printInner() ;
}
}

Output:

Example of Member Inner Class

[2] LOCAL INNER CLASS

A class which is created inside a class and within a method is known as Local Inner Class. It cannot be instantiated outside the method. It means its scope is limited and can only be instantiated within the method.

Example:

class OuterClass {

public void method() {
String name = “W3TS” ;

class LocalInnerClass {
public void printLocal() {
System.out.println(“Example of Local Inner Class “+name) ;
}
}

LocalInnerClass innerobj = new LocalInnerClass() ;
innerobj.printLocal() ;
}

public static void main(String args[]) {
OuterClass outerobj = new OuterClass() ;
outerobj.method() ;
}
}

Output:

Example of Local Inner Class W3TS

[3] ANONYMOUS INNER CLASS

An inner class that doesn’t have a class name is known as anonymous inner class. The compiler decides the name for this class. It can be used to override method of a class or an interface. These classes are declared and instantiated simultaneously.

Syntax:

AnonymousInnerClass innerobj = new AnonymousInnerClass() {
public void method() {
}

Example 1 :

abstract class AnonymousInnerClass {
public abstract void classmethod() ;
}

class OuterClass {
public static void main(String args[]) {
AnonymousInnerClass innerobj = new AnonymousInnerClass() {
public void classmethod() {
System.out.println(“Example of Overriding an Abstract Class using an Anonymous Inner Class”) ;
}
;
innerobj.classmethod() ;
}
}

Output 1:

Example of Overriding an Abstract Class using an Anonymous Inner Class

Example 2:

interface AnonymousInner{
public void method() ;
}
class AnonymousInnerClass{
public static void main(String args[]) {
AnonymousInner innerobj = new AnonymousInner() {
public void method() {
System.out.println(“Example of Overriding an Interface using an Anonymous Inner Class”) ;
}
;
innerobj.method() ;
}
}

Output 2:

Example of Overriding an Interface using an Anonymous Inner Class

We have provided you the description on the use of Nested Classes in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest