How to Implement Nested Class in Java?

Swatee Chand
Edureka
Published in
6 min readSep 20, 2019

--

In Java, a class can be defined within another class and such classes are known as nested classes. These classes help you to logically group classes that are only used in one place. This increases the use of encapsulation and creates a more readable and maintainable code. This blog on “Nested Class in Java” will give you a quick to-the-point introduction to nested classes in the Java language. Below are the topics covered in this blog:

  • Nested Class in Java
  • Types of nested classes
  • Difference Between Static and Non-static Nested Classes
  • Key Points to Remember

Nested Class in Java

The class written within a class is called the nested class while the class that holds the inner class is called the outer class. Below are some points to remember for nested classes in Java -

  • The scope of a nested class is bounded by its enclosing class.
  • A nested class has access to the members of the class in which it is nested. But, the enclosing class cannot access the members of the nested class.
  • A nested class is its enclosing class member.
  • A nested class can be declared public, private, protected, or package-private.

Types of nested classes

Inner/Non-static nested class: In Java, non-static classes are a security mechanism. A class cannot be associated with the access modifier private, but if you have the class as a member of other class, then the non-static class can be made private.

Types of inner classes −

  • Inner Class
  • Method-local Inner Class
  • Anonymous Inner Class

Inner Class

To create an inner class you just need to write a class within a class. An inner class can be private which cannot be accessed from an object outside the class. Below is a program to create an inner class. In this example, the inner class is made private and is accessed class through a method.

class Outer_Test {
int num;

// inner class
private class Inner_Test {
public void print() {
System.out.println("This is an Our inner class");
}
}

// Accessing he inner class from the method
void display_Inner() {
Inner_Test inner = new Inner_Test();
inner.print();
}
}

public class My_class {

public static void main(String args[]) {
// Instantiating the outer class
Outer_Test outer = new Outer_Test();

// Accessing the display_Inner() method.
outer.display_Inner();
}
}

Output

Method-local Inner Class

In Java, a class can be written within a method and it is a local type. Similar to local variables, the scope of an inner class is restricted within the method. A method-local inner class is incorporated only within the method where the inner class is defined. The below program shows how to use a method-local inner class.

public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 1001;

// method-local inner class
class StarInner_Test {
public void print() {
System.out.println("This is star inner class "+num);
}
} // end of inner class

// Accessing the inner class
StarInner_Test star = new StarInner_Test();
star.print();
}

public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.my_Method();
}
}

Output

Anonymous Inner Class

Anonymous inner class is an inner class declared without a class name. In an anonymous inner class, we declare and instantiate it at the same time. They are generally used when you need to override the method of a class or an interface. The below program shows how to use an anonymous inner class -

abstract class AnonymousInnerTest {
public abstract void mytest();
}

public class Outer_class {

public static void main(String args[]) {
AnonymousInnerTest inner = new AnonymousInnerTest() {
public void mytest() {
System.out.println("This is an example of anonymous inner test class");
}
};
inner.mytest();
}
}

Output-

Static nested class:

A static class is a nested class that is a static member of the outer class. Unlike inner class, the static nested class cannot access member variables of the outer class because the static nested class doesn’t require an instance of the outer class. Hence, there is no reference to the outer class with OuterClass.this. The syntax of a static nested class is –

class MyOuter {
static class Nested_Test {
}
}

Example of Static Nested Class

public class Outer {
static class Nested_Test {
public void my_method() {
System.out.println("This is Edureka's nested test class");
}
}

public static void main(String args[]) {
Outer.Nested_Test nested = new Outer.Nested_Test();
nested.my_method();
}
}

Output

Difference Between Static and Non-static Nested Classes

Static nested classes do not have access to other members of the enclosing class directly. Being static, it must access the non-static members of its enclosing class through an object which means it cannot refer to the non-static members of its enclosing class directly. And due to this restriction, static nested classes are seldom used.

Non-static nested classes have access to all members of its outer class and can refer to them directly in the same way that other non-static members of the outer class do.

Before we come to the end of this article, let us look at a few key points.

Key Points to Remember

  • The inner class is treated as a regular member of a class.
  • Since the inner class is members of the outer class, you can apply different access modifiers like protected, private to your inner class.
  • Since the Nested class is a member of its enclosing class, you can use . (dot) notation in order to access the nested class and its members.
  • Using a nested class makes your code more readable and provides better encapsulation.
  • The inner class has access to other members of the outer class, even if they are declared private.

With this, we come to an end of this blog on Nested classes in Java. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Java Tutorial

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on September 20, 2019.

--

--