Sitemap

Member-only story

Java Nested Classes — Static Versus Inner

6 min readJan 4, 2024

--

In this article, we will see the main difference between static versus non-static Nested classes. A non-static nested class is also called as an Inner class.

Press enter or click to view image in full size
Photo by Caspar Camille Rubin on Unsplash

Also check out my YouTube Channel at https://www.youtube.com/@viraj_shetty and subscribe for quality software content.

Nested Classes — static

What is a Nested Class? Most classes that we define are Top level classes but Java allows you to create a class “within” another class. There are many different reasons for this. Sometimes a class is relevant only to one other class. By declaring it within that class and making it private, such a class will not be exposed to other classes. Its a great example of encapsulation. You may also group related classes of a component as nested classes.

Let’s take an example of a static Nested class.

public class Parent {

private final String first;
private final String last;

public static class Child {

private final String first;

private Child(String first) {
this.first = first;
}

public String getFullName() {
return first;
}
}

public Parent(String first, String last) {
this.first = first;
this.last = last;
}

public void createChild(String first) {
Child c = new…

--

--