Static nested class

If you are not familiar with java nested class, then you must read it first. Because static nested class is part of nested class in java. Some of the programmers refer to it as java static inner class. In this post, we will read how we can use it and also discuss the need of static class.
We can’t declare the outer class as a static class. So we can read the static nested class in more detail from here (static class in java).

Here is the table content of the article will we will cover this topic.
1. What is the static nested class?
2. Important points about the static nested class?
3. Access of Static nested class?
4. Creating an object of static nested class?

5. Properties of static class?
6. Declaring a Static Class?
7. How to create an object of static class?
8. Accessing Static Class Members from Static nested Classes?
9. How does static class work?
10. Key Points to Remember?
11. What is the need of static class?

What is the static nested class?

As we know we can declare a class within another class, such classes are known as nested classes. It is also known as Java static class. So that we can declare a nested class with the static keyword, and it is called a static nested class. We can’t declare outer class as a static class. So we can read the static nested class in more detail from here (static class in java).

Java static class

Important points about it?

  • In Java, static class can’t access non-static variables and methods.
  • It can be accessed by an outer class name.
  • It can access the static variables and static methods of outer class including private.
class OuterClassName
{
   // variables and method of outer class
   static class className
   {
	// variables and method of static class
   }
}
class College
{
	// variables and method of College class
	static class Student
        {
		// variables and method of Student class
        }
}

Access of Static nested class

You can’t directly access the nested static class. It can access by the use of an outer class.

OuterClassName.StaticNestedClassName;
College.Student;

Creating an object of static nested class

You can create an object of the static nested class by use of an outer class.

OuterClassName.StaticNestedClassName objectName = new outerClassName.StaticNestedClassName();
College.Student object = new College.Student();

Let’s take an example of a static nested class. Here College is Outer class and Student is a static nested class. We declared some static and non-static variables and methods. Some of them are accessible and rest are not able to access.

class College
{
   // Non static variable
   int nonStaticVariableOfCollege = 5;

   // static variable
   static int staticVariableOfCollege = 10;

   // private and static variable
   private static int privateAndStaticVariableOfCollege = 15;

   // Non- static method
   void nonStaticShowMethod()
   {
      System.out.println("This is Non static method of college class");
   }

   // static method
   static void staticShowMethod()
   {
     System.out.println("This is static method of college class");
   }

   //private and static method
   private static void privateStaticShowMethod()
   {
     System.out.println("This is private and static method of college class");
   }

   // static nested class
   static class Student
  {
    // Non static method of Student class
    void display()
   {
       // You can't access the non static variables of College class.
       //System.out.println("You can't access the non static variable      
        ="+nonStaticVariableOfCollege);

       // You can access the static variables of College class.
       System.out.println("You can access the static variable ="+staticVariableOfCollege);

       // You can access the private static variables of College class.
       System.out.println("You can access the priavte static variable 
       "+privateAndStaticVariableOfCollege);

        // You can access the static method of College class.
       staticShowMethod();

       // You can access the private static method of College class.
       privateStaticShowMethod();

       // You can't access the non static method of College class.
       // nonStaticShowMethod();
}

// Non static method of Student class
static void displayData()
{
System.out.println("This is static method of Student class");
}
              }
}


public class MainClass
{
    public static void main(String[] args)
    {
          // accessing a static method of Student class
          College.Student.displayData();

         // Accessing of non-static method we must create object of Student
        College.Student object = new College.Student();
        // Accessing of non static method
        object.display();
      }
}

Output:This is static method of Student class
You can access the static variable =10
You can access the priavte static variable 15
This is static method of college class
This is private and static method of college class

As you see in the above example, the static method of a nested class can access by use of the class name and non-static are accessed by an object.

Leave a Comment