Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

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 −

class MyOuter {
   static class Nested_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.

package com.tutorialspoint;

public class Outer {
   static class NestedDemo {
      public static void print() {
         System.out.println("This is my nested class");
      }
   }
   
   public static void main(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.

package com.tutorialspoint;

public class Tester {
   public static void main(String[] arguments) {
	   Outer.NestedDemo.print();
   }
}

class Outer {
   static class NestedDemo {
      public static void print() {
         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.

package com.tutorialspoint;

public class Tester {
   public static void main(String[] arguments) {
	   new Outer.NestedDemo().print();
   }
}

class Outer {
   static class NestedDemo {
      public void print() {
         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
Advertisements