java static class

Java Static Class Tutorial

Java Static Class Static is a well-known keyword in java programming language. The static keyword can be used with class, method, variable and block. But static keyword behaves differently in every situation. Static members directly belong to the class. In simple terms, we don’t need to create an object of a class to access static members of the class. But for better understanding, you can take the help of the given example.

We cannot use the static keyword directly with class. To use static keyword with class, we have to make one inner class. Because Java does not allow to make the outer class as static. Only nested or inner class can be static. It can be accessed without instantiating the outer class, using other static members.

Java Static class Syntax

class MyTest {
   static class Nested_Test {
   }
}

Example: In the following example, we have created one outer class whose name is MyTestOuter. In this class one private static member “name”. Inside Outer class one inner class is there whose name is MyTestInnerClass.

One main class is there MyMainClass. This class contains the main method.

public class MyTestOuter {
    private static String name = "DEVELOPER HELPS";

    public static class MyTestInnerClass {
        public void testMethod1() {
            System.out.println("MyTestOuter Class Name: " + name);
        }
    }

    public static void main(String[] args) {
        MyTestInnerClass obj = new MyTestInnerClass();
        obj.testMethod1();
    }
}

class MyMainClass {
    public static void main(String... s) {
        // This code can be used to execute MyTestOuter's main method if needed.
        // MyTestOuter.main(s);
    }
}


Output :

MyTestOuter Class Name: DEVELOPER HELPS

Java Static Block

Static block is also a very important part of Java programming. A static block is used to initialize static members of the class. Because the Static block executes when the class is loaded in the memory.  A class may have multiple static blocks. Multiple static blocks will be executed as they have written.

Example: In MyTestOuter, two data members one is static and the other one is non-static. We can only initialize static data members within the static block.

public class MyTestOuter {
    static int num;
    String name;

    MyTestOuter() {
        System.out.println("MyTestOuter Constructor");
    }

    static {
        num = 11;
        System.out.println("Static Block Executed & number is: " + num);
    }

    public static void main(String[] args) {
        MyTestOuter obj = new MyTestOuter();
    }
}


Output:

Static Block Executed & number is: 11
MyTestOuter Constructor

Multiple Static Blocks

Example: Multiple static is possible in java programming. It called before the constructor. Because it loads when the class is loaded in memory.

public class MyTestOuter {
    static int num;
    String name;

    MyTestOuter() {
        System.out.println("MyTestOuter Constructor");
    }

    static {
        num = 11;
        System.out.println("Static Block 1 Executed & number is: " + num);
    }

    static {
        num = 12;
        System.out.println("Static Block 2 Executed & number is: " + num);
    }

    public static void main(String... s) {
        MyTestOuter obj = new MyTestOuter();
    }
}

class MyTestClass {
    public static void main(String... s) {
        MyTestOuter obj = new MyTestOuter();
    }
}


Output:

Static Block 1 Executed & number is: 11
Static Block 2 Executed & number is: 12
MyTestOuter Constructor 

Java Static Methods

Static keyword also used with Methods. Static methods have some benefits. Because we can call static methods without the creation of the object. With the help of the Class name, we can access static methods. Static variables are directly accessible from static methods. Because it’s the functionality of the static keyword.

Note: For more Tutorials please visit Java Tutorials.

Example:

public class MyTestOuter {
    static int num;
    String name;

    public static void MyTestMethod1() {
        num = 11;
        System.out.println("Static Block 1 Executed & number is: " + num);
    }

    public void MyTestMethod2() {
        name = "DEVELOPER HELPS";
        num = 12;
        System.out.println("Block 2 Executed & number is: " + num);
        System.out.println("Block 2 Executed & name is: " + name);
    }

    public static void main(String... s) {
        // Calling Static Method with the help of class name
        MyTestOuter.MyTestMethod1();

        // Calling Static Method
        MyTestOuter obj = new MyTestOuter();
        obj.MyTestMethod2();
    }
}


Output:

Static Block 1 Executed & number is: 11
Block 2 Executed & number is: 12
Block 2 Executed & name is: DEVELOPER HELPS

Thanks for reading the post. If you have faced any issue in the post, please do comment. Because of your comment, we can improve the content and provide the best material in the future. So please like and share the post.

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!