Java – Static Classes

In Java concept of static class is introduced under concept of inner classes, which are specially designed for some delicate functionality in a class.

Java Static Classes

Static classes in Java are allowed only for inner classes which are defined under some other class, as static outer class is not allowed which means that we can’t use static keyword with outer class.

Static classes are defined the same as other inner classes in Java only with a static keyword in front of its name. These classes have some unique characteristics that make them differ from other non-static inner classes.

Features of Java Static Classes

The following are the features of static classes in Java:

  • Static class do not need to create an instance of outer containing class in order to create its own instance.
  • Static class can access members(variables/methods) of outer containing class only if they are static in nature.Which means that a static nested class does not have access to the instance variables and methods of the outer class.

Syntax of Java Static Class

The syntax of static nested class is as follows −

classMyOuter{staticclassNested_Demo{}}

Instantiating a static nested class is a bit different from instantiating an inner class. The following programs show how to use a static nested class in multiple cases.

Example of Java Static Class

In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. In main() method, we’re using static method of static class directly without any reference as main is part of Outer class.

packagecom.tutorialspoint;publicclassOuter{staticclassNestedDemo{publicstaticvoidprint(){System.out.println("This is my nested class");}}publicstaticvoidmain(String args[]){NestedDemo.print();}}

If you compile and execute the above program, you will get the following result −

Output

This is my nested class

Java Static Class: More Examples

Example 1

In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. As main method is in different class, we’re accessing the static inner class using Outer class.

packagecom.tutorialspoint;publicclassTester{publicstaticvoidmain(String[] arguments){Outer.NestedDemo.print();}}classOuter{staticclassNestedDemo{publicstaticvoidprint(){System.out.println("This is my nested class");}}}

If you compile and execute the above program, you will get the following result −

Output

This is my nested class

Example 2

In this example, we’ve created a class Outer and an inner static class as NestedDemo within it. Now as static class has instance method and main method is in different class, we’re accessing the static inner class using Outer class object.

packagecom.tutorialspoint;publicclassTester{publicstaticvoidmain(String[] arguments){newOuter.NestedDemo().print();}}classOuter{staticclassNestedDemo{publicvoidprint(){System.out.println("This is my nested class");}}}

If you compile and execute the above program, you will get the following result −

Output

This is my nested class

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *