Java Static Nested Classes

Static Nested Classes or Static Inner Classes in Java

Static nested class in Java is simply a class that is declared as static member of the enclosing class. A static nested class in Java is simply a class scoped within another class. Static nested class as such shares no relationship with enclosing class.

For a Java static nested class the static modifier says that the nested class can be accessed, as with other static members, without having an instance of the outer class. And to create an object of static nested class you don't need the static nested class to have a reference to the outer class object.

Static nested class is the simplest form of nested class. A static nested class is declared with the static modifier. When a static nested class is nested in an interface, it is always static and the static modifier is, by convention, omitted. A static nested class acts just like any top-level class. It can extend any other class (including the class it is a member of), implement any interface and itself be used for further extension by any class to which it is accessible. A static nested class can be declared final or abstract, just as a top-level class can, and it can have annotations applied to it.

Application Use of Static Nested Classes

A static nested class in Java serves a great advantage to namespace resolution. This is the basic idea behind introducing static nested classes in Java. For example, if you have a class with an exceedingly common name, and in a large project, it is quite possible that some other programmer has the same idea, and has a class with the same name you had, then you can solve this potential name clash by making your class a public static nested class. And your class will be known as outer class, followed by a period (.), and then followed by static nested class name. Let's take an example how static nested class is declared and used in a program.

How to Code Static Nested Classes?

To make use of Java's static nested class we won't need to create an object of outer class in order to create an object of static nested class. But, the syntax of creating an object of static nested class differs a little from the usual syntax. In this case you need to follow OuterStatic.InnerStatic syntax. Though, the following example demonstrating static nested class would not compile because the static nested class tries to access non-static member mem of the OuterStatic class that is not permitted. So to compile and execute the following code just remove or comment out the statement System.out.println(mem); and you will get 50 as output.

/* StaticClassDemo.java */
 
class OuterStatic
{
    private int mem = 20;
    private static int smem = 50;
 
    static class InnerStatic
    {
        public void accessMembers ()
        {
            System.out.println(mem); //Error: Cannot make a static reference to the non-static field mem
            System.out.println(smem);
        }
    }
}
 
public class StaticClassDemo
{
    public static void main(String[] args)
    {
        OuterStatic.InnerStatic is = new OuterStatic.InnerStatic();
        is.accessMembers();
    }
}

Remember that a static nested class cannot access non-static members of the outer class, because it does not have an implicit reference to any outer instance. Because a static class is not an inner class so it does not share any special relationship with an instance of the outer class.

Last Word

However, sometimes, it may be confusing for you that inner classes and nested classes are same or different. All inner classes are nested classes too, but not all nested classes are inner classes. Static inner classes are an example of nested class but not an inner class. By the standard definition of inner classes, static nested classes are not really inner classes.

In this tutorial we talked of static nested class or static inner class. Static nested classes are not really inner classes; their sole purpose is to serve structuring and scoping mechanism for logically related types. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.