AppDividend
Latest Code Tutorials

Java Static Class Example | Static Class In Java Tutorial

0

Java Static Class Example | Static Class In Java Tutorial is today’s topic. In Java, the static class is always implemented in the nested inner class. Suppose there are two classes, outer and inner, in which inner is the nested class. Then static can be given to the inner class only. The benefit of the static class is we didn’t create an object for a static class in the main method.

Java Static Class Example

Static classes are the way of grouping classes together in Java language. Java doesn’t allow you to create the top-level static classes; only nested static inner classes. Only nested classes can be static. Static keyword can be used with the class, variable, method, and block. Static members belong to a class instead of the particular instance, and this means if you make the member static, you can access it without the object.

#Differences between static and non-static class

  1. In nested static class, we don’t need a reference of the outer class, but non-static nested class always requires the reference of the outer class.
  2. The static nested class can only access the static method and members of the outer class while a non-static nested class can access both static and non-static members of the outer class.
  3. We create the object of the non-static class in the main method and use that object as the reference of the nested (inner) class. We never create the object of the static class in the main method.

See the following program.

class outer {
  static class inner {
    void display() {
      System.out.println("this is static class");
    }
  }

  public static void main(String args[]) {
    outer.inner obj = new outer.inner(); // we only created the object of outer class
    obj.display();
  }
}

See the following output.

 

Java Static Class Example

Okay, now let’s see an example of a non-static class in Java. See the following code.

class nextOuter {
  class nextinner {
    void show() {
      System.out.println("this is non-static class");
    }
  }

  public static void main(String args[]) {
    nextOuter outer = new nextOuter();
    nextOuter.nextinner inner = outer.new nextinner();
    inner.show();
  }
}

See the following output.

 

Differences between static and non-static class

Also, see another example.

class StrangerThings {

    public static class El {
        public El() {
            System.out.println("Eleven has telekinetic power");
        }
    }

    public StrangerThings() {
        System.out.println("StrangerThings Season 3 is awesome");
    }
}

public class SC {

    public static void main(String[] args) {
        StrangerThings str = new StrangerThings();
        StrangerThings.El el = new StrangerThings.El();
    }
}

I’ve added the constructors to both classes so that we can see when they are instantiated in other words, when those objects are created from them. We can use the StrangerThings class in the usual way; note that creating a StrangerThings object does not create an El object. 

If we want to instantiate our static class, creating an object from our static El class, we have to use the new separately on the class.

See the output.

➜  java javac SC.java
➜  java java SC
StrangerThings Season 3 is awesome
Eleven has telekinetic power
➜  java

As you can see, the static inner El class acts like an entirely separate class, which happens to be accessed via the outer StrangerThings class. We don’t need the instance of the outer class to create an object of a static inner class. 

The only exception to static classes acting like completely separate classes to their enclosing classes is that static inner classes can access static data members of the enclosing class — or call static methods, for that matter.

So, A class can be made static only if it is a nested class.

  1. Nested static class doesn’t need the reference of Outer class.
  2. A static class cannot access non-static members of the Outer class.

Finally, Static Class In Java Tutorial is over.

Leave A Reply

Your email address will not be published.

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