Home » Static nested class in Java

Static nested class in Java

by Online Tutorials Library

Java static nested class

A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.

  • It can access static data members of the outer class, including private.
  • The static nested class cannot access non-static (instance) data members or

Java static nested class example with instance method

TestOuter1.java

Test it Now

Output:

data is 30  

In this example, you need to create the instance of static nested class because it has instance method msg(). But you don’t need to create the object of the Outer class because the nested class is static and static properties, methods, or classes can be accessed without an object.

Internal class generated by the compiler

Java static nested class example with a static method

If you have the static member inside the static nested class, you don’t need to create an instance of the static nested class.

TestOuter2.java

Test it Now

Output:

data is 30  

Next TopicNested Interface

You may also like