Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top-level classes, Nested classes can be Static. Non-static nested classes are also known as Inner classes.
Note: The top level class cannot be static in java, to create a static class we must create a nested class and then make it static.
An instance of an inner class cannot be created without an instance of the outer class. Therefore, an inner class instance can access all of the members of its outer class, without using a reference to the outer class instance. For this reason, inner classes can help make programs simple and concise.
Remember: In static class, we can easily create objects.
Differences between Static and Non-static Nested Classes
The following are major differences between static nested classes and inner classes.
- A static nested class may be instantiated without instantiating its outer class.
- Inner classes can access both static and non-static members of the outer class. A static class can access only the static members of the outer class.
Example of Static and Non-static Nested Classes
Below is the implementation of topic mentioned above:
Java
// Java program to Demonstrate How to
// Implement Static and Non-static Classes
// Class 1
// Helper class
class OuterClass {
// Input string
private static String msg = "GeeksForGeeks";
// Static nested class
public static class NestedStaticClass {
// Only static members of Outer class
// is directly accessible in nested
// static class
public void printMessage()
{
// Try making 'message' a non-static
// variable, there will be compiler error
System.out.println(
"Message from nested static class: " + msg);
}
}
// Non-static nested class -
// also called Inner class
public class InnerClass {
// Both static and non-static members
// of Outer class are accessible in
// this Inner class
public void display()
{
// Print statement whenever this method is
// called
System.out.println(
"Message from non-static nested class: "
+ msg);
}
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating instance of nested Static class
// inside main() method
OuterClass.NestedStaticClass printer
= new OuterClass.NestedStaticClass();
// Calling non-static method of nested
// static class
printer.printMessage();
// Note: In order to create instance of Inner class
// we need an Outer class instance
// Creating Outer class instance for creating
// non-static nested class
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner
= outer.new InnerClass();
// Calling non-static method of Inner class
inner.display();
// We can also combine above steps in one
// step to create instance of Inner class
OuterClass.InnerClass innerObject
= new OuterClass().new InnerClass();
// Similarly calling inner class defined method
innerObject.display();
}
}
OutputMessage from nested static class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Similar Reads
Static Blocks in Java In simpler language whenever we use a static keyword and associate it to a block then that block is referred to as a static block. Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the
4 min read
Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl
5 min read
Static import in Java In Java, static import concept is introduced in 1.5 version. With the help of static import, we can access the static members of a class directly without class name or any object. For Example: we always use sqrt() method of Math class by using Math class i.e. Math.sqrt(), but by using static import
4 min read
Static Control Flow in Java Static Control Flow decides the sequence of activities/steps that will be executed in order when we run a java class that contains static variables, methods, and blocks. This article will explain how static control flow occurs whenever a Java program is executed. Prerequisite: Static Blocks The Stat
4 min read
Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
8 min read