Static Class in Java

by | Jan 6, 2023 | Java

Can a class be Static in Java?

The short answer is yes, and Java allows static classes to be created. Static Instance variables, Static Methods, as well as Static Blocks are supported in Java along with Static Classes.

The concept of Nested classes arises when one class is defined inside another class. Such classes are called nested classes. The outer class, which contains another class inside, is called the Outer Class, ad the class inside another class is called the Inner class. Unlike top-level classes, Inner classes can also be static. Non–static nested classes mentioned above are known as Inner classes.

Since the inner class resides inside the boundaries of the outer class, when you want to create an instance of the inner class, it goes without saying that you will also have to instantiate the outer class. It also follows that all the methods of the outer class can be accessed by the inner class without having to create a reference to the outer class explicitly. For this particular reason, the inner classes can help make programs simple and concise.

What are the differences between static and non-static nested classes?

The differences between static and non-static classes are as follows:

  1. If you want to instantiate, that is, create an object of a nested class, you can do so without having to instantiate the outer class inside which it resides.
  2. Inner classes can access both static and non-static members of the outer class. However, a static class can only access the static members of the outer class.

Sample Code

//This program will show how you can create both static and non static classes in Java
class OuterClass
{
private static String msg = “ Hello World ”;
//Nested classes declared as static
pubic static class NestedSClass
{
//in a nested class, you ca only access the static members present in the outer class
public void printMessage ()
{
//we will get a compiler error here if we try to change ‘message’ to a non static variable here.
System.out.println (“ Output from nested static class: ” + msg);
}
}
//This is the inner class which is the non static class of the nested classes
public class InnerClass
{
//Since these are non static classes, you can easily access all member fields and methods of both the inner and the outer classes
{
public void display ()
{
System.out.println (“ Output from non static nested class :” + msg);
}
}
}
class Main
{
//object creation of the static and non static nested classes
public static void main (String args [])
{
//object creation
OuterClass.NestedSClass printer = new OuterClass.NestedSClass ();
printer.printMessage ();
//An object of the inner class can only be created if it is preceded with an object of the outer class. //Object creation of the outer class first followed by inner class.
OuterClass outer = new OuterClass ();
OuterClass.InnerClass inner = outer.new InnerClass ();
inner.display ();
//The object creation step of both outer and inner classes in one sentence.
OuterClass.InnerClass innerObject = new OuterClass ().new InnerClass ();
innerobject.siaply ();
}
}

 

OUTPUT

Output from nested static class: Hello World

Output from non static nested class: Hello World

Output from non static nested class: Hello World

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.