Difference Between Static and Non Static Nested Class in Java Last Updated : 19 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between making a class static or non-static. There are two kinds of classes in Java, one is called a top-level class and the other is called a nested class. As the name suggested top-level class is a class that is declared in '.java' file. On the other hand, a nested class is declared inside another class. The class which enclosed nested class is known as Outer class. In the Java programming language, you can not make a top-level class static. You can only make nested classes either static or non-static. If you make a nested class non-static then it also referred to as Inner class. User-cases: Static nested classNon-static nested class Example 1: Static nested class Java // Java Program to illustrate // static nested class // Importing input output classes import java.io.*; // Importing all classes from // java.util package import java.util.*; // Class 1 // Helper class1 class ClassA { // Static variable static int x = 10; int y = 20; // Static variable with private // access modifier in ClassA private static int z = 30; // Class 2 - Nested static class // Helper Class 2 static class ClassB { // Method of nested static class void get() { // Execution statements whenever // function is called // Print and display commands System.out.println("x: " + x); System.out.println("z: " + z); } } } // Class 3 // Main Class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of static nested class // defined outside Main class ClassA.ClassB ob1 = new ClassA.ClassB(); // Calling the method of static nested class // in main() method ob1.get(); } } Outputx: 10 z: 30 Example 2: Non-static nested class Java // Importing all input output classes import java.io.*; // Class 1 // Helper Class 1 class ClassA { // Static member variable of ClassA static int x = 10; int y = 20; public int z = 30; // Class 2 - Helper Class 2 // Non-static nested class class ClassB { // Method of Non-static nested class void get() { // Execution command of get() method // Print and display commands System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("z: " + z); } } } // Class 3 // Main Class class GFG { // Main driver method public static void main(String[] args) { // Creating object of Class 1 in main() method ClassA ob1 = new ClassA(); // Creating object of non-static nested class in // main() ClassA.ClassB ob2 = ob1.new ClassB(); // Calling non-static nested class method // in main() method ob2.get(); } } Outputx: 10 y: 20 z: 30 Now the difference between Static and Non-Static Nested Class in Java: The static inner class can access the static members of the outer class directly. But, to access the instance members of the outer class you need to instantiate the outer class.Nested static class doesn't need a reference of Outer class but a nonstatic nested class or Inner class requires Outer class reference.A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.Another difference between static and non-static nested class is that you can not access non-static members e.g. method and field into nested static class directly. If you do you will get errors like "nonstatic member can not be used in the static context". While the Inner class can access both static and non-static members of the Outer class. Comment More infoAdvertise with us Next Article Difference between static and non-static variables in Java K kaaruni1124 Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Practice Tags : Java Similar Reads Difference between static and non-static method in Java A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access 6 min read Difference Between Singleton Pattern and Static Class in Java Singleton Pattern belongs to Creational type pattern. As the name suggests, the creational design type deals with object creation mechanisms. Basically, to simplify this, creational pattern explains to us the creation of objects in a manner suitable to a given situation. Singleton design pattern is 6 min read Difference between static and non-static variables in Java In Java, variables are mainly categorized into two main types based on their working and memory allocation, and these two variables are static variables and non-static variables. The main difference between them is listed below:Static variables: These are variables that are shared among all the inst 3 min read Difference Between Class.this and this in Java In java, Class.this and this might refer to the same or different objects depending upon the usage. this this is a reference variable that refers to the current object. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Class.this Cla 3 min read Difference Between Constructor and Static Factory Method in Java Whenever we are creating an object some piece of code will be executed to perform initialization of that object. This piece of code is nothing but a constructor, hence the main purpose of the constructor is to perform initialization of an object but not to create an object. Let us go through the bas 4 min read Difference between Instance Variable and Class Variable Instance Variable: It is basically a class variable without a static modifier and is usually shared by all class instances. Across different objects, these variables can have different values. They are tied to a particular object instance of the class, therefore, the contents of an instance variable 2 min read Like