Konstantina Dimtsa

About Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java static class Example

In this example, we will discuss about static classes in Java. First of all, let’s give a short explanation of the static modifier. For example, if a field or a method in a class has the static modifier in its declaration , then it is always associated with the class as a whole, rather than with any object of the class.

In the code below we have declared a class named Vehicle, a class field member named vehicleType and a method named getVehicleType(), both declared as static.
 
 
 

public class Vehicle {

    private static String vehicleType;
    
    public static String getVehicleType(){
        return vehicleType;
    }

}

The static modifier allows us to access the variable vehicleType and the method getVehicleType() using the class name itself, as follows:

Vehicle.vehicleType
Vehicle.getVehicleType()

Something similar happens to classes that are declared static, but in order to explain this better, we must first explain the inner classes or generally, the nested classes, because ONLY nested classes can be static.

Java supports the concept of nested classes. Nested classes are classes that can be defined within the body of another class. An example of a nested class is illustrated below:

class OuterClass {
    //code
    class NestedClass {
        //code
    }
}

The OuterClass which contains another class can be also called top-level class.

Nested classes are further divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are just called inner classes.

class OuterClass {
    //code
    static class StaticNestedClass {
        //code
    }
    class InnerClass {
        //code
    }
}
  • What’s the difference between nested static classes and non-static (inner) classes?
  • The main difference is that inner class requires instantiation of the outer class so as to be initialized and it is always associated with an instance of the enclosing class. On the other hand nested static class is not associated with any instance of the enclosing class. Nested static classes are declared with the static keyword, which means than can be accessed like any other static member of class, as we shown before.

1. Example of nested classes

Create a java class named OuterClass.java with the following code:

OuterClass.java

package com.javacodegeeks.javabasics.staticclass;

public class OuterClass {

    public static void main(String[] args) {

        //nested static class doesn't need instantiation of the outer class
        OuterClass.NestedStaticClass nestedStatic = new OuterClass.NestedStaticClass();
        nestedStatic.printMethodNestedStatic();

        //inner class needs instantiation of the outer class
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.printMethodInner();
    }

    // Static nested class
    public static class NestedStaticClass {

        public void printMethodNestedStatic() {
            System.out.println("Method of nested static class");
        }
    }

    // inner (non-static nested) class
    public class InnerClass {

        public void printMethodInner() {
            System.out.println("Method of inner(non-static nested) class");
        }
    }

}

As we can observe in the above code, we have declared a class named OuterClass which is considered to be the outer class or otherwise, the top level class. Also, we can see that we have declared two more nested classes (they are enclosed in the outer class), one of which is static and the other one is non-static. Finally, we have declared a main method in the outer class, which is static as always (Really, why is that? The answer is that we can call it without creating any instance of class which contains this main method). We can see that the method of the nested static class can be accessed without creating an object of the OuterClass, whereas the method of the inner class needs instantiation of the OuterClass in order to get accessed.

  • Output:
Method of nested static class
Method of inner(non-static nested) class

2. Download the source code

This was an example for static class in Java. You can download the source code from here: StaticClassExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close