A simple guide to using nested classes in Java

A nested class is a class that is defined in another class. It is a member of the outer class, so it can use any of the four visibility modifiers, as can the class fields and methods.

In this Java programming tutorial, developers will learn how to use nested classes and others Object-oriented programming (OOP) Concepts.

What are nested classes in Java?

Nested classes have access to all members of the outer class, including those declared private. Note, however, that the opposite is not the case; outer classes cannot access members of the inner class.

Nesting classes enables the programmer to logically group classes that are only used by certain classes. In addition, the nesting makes it easy to maintain the code – a core principle of OOP programming.

The following sections show you how to use the four types of nested classes in Java.

Read: Understand Java Objects and Java Classes

What is a static nested class in Java?

A nested class can be either static or non-static. A static class is defined with the static keyword. Such a class can only access the instance members of the outer class through object referencing. However, it can directly access the static members of the enclosing class.

The syntax for writing a static nested class in Java can be seen in the following code example:

Class OuterClass {static class NestedStaticClass {}}

You create an instance of a static nested class in the usual way that you instantiate other top-level classes:

NestedStaticClass nestedObj = new NestedStaticClass ();

The following sample code shows how to access static members and instance members of an outer class from a static nested class in Java:

public class OuterClassStaticExample {int x = 15; static double y = 178.5; static class StaticNestedClass {void show (OuterClassStaticExample outside) {System.out.println (” nThe instance field x of OuterClass is” + outside.x); System.out.println (” nThe static field y of OuterClass is” + y); }} public static void main (String args[]) {StaticNestedClass inner = new StaticNestedClass (); OuterClassStaticExample externalStatic = new OuterClassStaticExample (); (outerStatic); }}

Read: String objects in Java

How do you use an inner class in Java?

A non-static nested class (also called an inner class) is a class that is defined without the static keyword. It can directly access the members of an outer class. An inner class cannot declare static members because it is associated with an instance and not a class. Here is the syntax of an inner class in Java:

Class OuterClass {Class InnerClass {}}

It is also possible to instantiate an inner class. However, you can only create and use objects of an inner class within an outer class.

To create an object of an inner one, you must first instantiate the outer class and then the inner class, as in the following code example:

OuterClass outerObject = new OuterClass (); OuterClass.InnerClass innerObject = InnerClass ();

Notice the syntax for creating an instance of the inner class. Consider the following example, calling the display () method of an inner class in an outer class using Java:

public class OuterClass {int x = 15; class InnerClass {void display () {System.out.println (” nThe variable x of OuterClass is” + x); }} public static void main (String args[]) {Outer class outer = new outer class (); OuterClass.InnerClass inner = InnerClass (); inner.display (); }}

Read: The best Java IDEs

What is a local inner class in Java?

You can also create a nested class within a block (for loop, while loop, if .. else, etc.). Such a class is called a local class. They are usually declared in methods:

// Code fragment void distance (int x, int y) {class LocalInnerClass {}}

A local nested class is an inner class because it cannot access static members of an outer class. This class can only access the last members of an outer class.

What is the anonymous inner class in Java?

An anonymous inner class is an inner class that has no name. It has the advantage of keeping your code short because both declaration and instantiation happen at the same time. A good place to use it would be if you ever want to use a local class. Here is the syntax:

ClassType AnonymousClass = new ClassType (args) {};

Or you can use:

InterfaceType AnonymousClass = new InterfaceType () {};

The declaration of an anonymous nested class consists of the following parts:

  • The class name or interface name that the anonymous class extends or implements
  • The new operator
  • Brackets for passing the constructor arguments, similar to instances in the usual classes.
  • The body of the class. Note that only method declarations are allowed, not statements.

Summary of nested classes in Java

Nested classes allow developers to group classes logically and also allow you to increase the level of encapsulation in your programming.

It’s also interesting to know that after compiling a class (Outer) that has a nested class (Nested), a class file called Outer $ Nested.class is also created. So make sure you broadcast it too when you deploy your outer class.

Read: A guide to constructor chaining in Java

Related posts

How to Use LaTeX for Technical Writing


How to work with archive files in Go


Navigating Web3: What does it mean for Game Developers?


Leave a Comment