Static Class in Java

A class can be Static class only if it is a nested class. You can’t declare the top level class as Static. The static class must be nested. Static class can’t access the non-static member of the top level class. The static class can be accessed without having an object of outer class.

  • A static class can access the only static members of the top level class (even they are private).
  • Static class can’t access the non-static members of the top level class.

 

Nested Class in Java:

Nested Classes are classes that can be defined within the body of another class. Nested classes allow us to group together a collection of logically related classes. It helps makes the code more readable and less complex.

The classes defined within the body of the class are known as nested classes and the class they are defined in is called the outer class.  Nested classes are somewhat different than sub-classes. Sub-classes cannot access private members of its parent class while nested classes can.

 

Video Tutorial of Automation Testing:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

Static Class in Java:

Only nested classes can be declared as static classes. If we try to declare an outer class as a static class, Compiler will throw an error message.  let’s see the example of the static class.

Output :

Hi, I am from EasyBix
Hi, I am Merrilla and I am from EasyBix

 

Benefits of Static Class:

A static class can access the Private Variables and Methods of its outer class. Static classes can be declared to remove restrictions on member classes of an outer class. If we think the outer class is too limiting and we want a member class to perform more functions, we use the “static” keyword to provide that added functionality.

A static class can never be instantiated. Static classes can’t directly access non-static members of a class. It can interact with them only through an object reference. This is both a drawback and an advantage of a static class.

 

6 Comments
  1. Karan 8:12 PM / August 4, 2016 - Reply

    Nice Tutorial, Thanks Author 🙂

  2. Rajyalakshmi 12:08 PM / August 22, 2016 - Reply

    HI, I have tried this code in eclipse but iam getting the following error

    Exception in thread “main” java.lang.Error: Unresolved compilation problems:
    Syntax error on token “void”, @ expected
    Syntax error on token “]”, :: expected after this token
    Syntax error, insert “enum Identifier” to complete EnumHeader

    at coreJava.OuterClass.main(StaticClass.java:29)

    • Anshul Chauhan 7:13 PM / August 22, 2016 - Reply

      Hi RajyaLakshi,

      I hope you copied the Package as well. Remove the first statement (Package statement) and execute the code.

      I have verified at my end and it’s working as expected.

      • Rajyalakshmi 11:13 AM / August 23, 2016

        Hi Anshul,

        I didn’t copied the code. I typed by my own and please verify the code and let me know where i was wrong.

        package coreJava;

        import java.io.*;
        import java.net.*;

        public class StaticClass {

        String name = “Rajya”;
        private static String company = “TNS Services”;

        static class staticCls()
        {
        public void display()
        {
        System.out.println(“HI, Iam from” + company);
        }

        }

        class InnerClass()
        {
        public void display() {
        System.out.println(“HI, Iam ” +name + “And Iam from ” + company);
        }

        }

        public static void main(String [] args)
        {
        StaticClass.staticCls stat=new StaticClass.staticCls();
        stat.display();

        StaticClass sc=new StaticClass();
        StaticClass.InnerClass inn=sc.new InnerClass();
        inn.display();
        }
        }

        and getting error at console as

        Exception in thread “main” java.lang.Error: Unresolved compilation problems:
        Syntax error on token “void”, @ expected
        Syntax error on token “]”, :: expected after this token
        Syntax error, insert “enum Identifier” to complete EnumHeader

        at coreJava.StaticClass.main(StaticClass.java:28)

      • Anshul 4:14 PM / August 24, 2016

        Hi Rajya,

        Why are you using method body with class like – static class staticCls() and innerClass() …. You are getting compilation error . So there is no way to execute code … First resolve your compilation errors

  3. Priyanka Sharma 12:14 PM / October 28, 2016 - Reply

    hi,
    my question is how can InnerClass uses static & non static variable of OuterClass widout being inherited from OuterClass??

Leave a Reply