What is Static in Java – Advantages and Restrictions

What is Static in Java?

  • Static is a keyword
  • Static defines a member that can be used without creating an instance of class.
  • Static members gets loaded when the particular class is loaded by class loader
  • Static are stored at Metaspace.

What is the use of Static in Java?

Memory Management

How does Static helps with Memory Management?

One example is, Static variables helps to save memory over local variables of a function due to multiple calls, for each call the allocation has to be made.

All static variables are allocated and initialized once at the start of the program and will last until the program is alive.

Static keyword can be used for,

  • Static Variable
  • Static Method
  • Static Class
  • Static Block

Let us look at each of these in detail,

Static Variable:

public class StaticExample {

    static String value = "value";
}

The variable declared along with static keyword. So how can this be accessed?

    public static void main(String args[]) {
        
        StaticExample.value //accessing static variables
    }

Can we have access modifiers for static variables?

Yes, we can have access specifiers for static.

    private static String value1 = "value";
    protected static String value2 = "value";

Can we modify static variables value?

Yes, we can modify the static variables value. (Please refer static block section)

Static Method:

We can also have a static method which gets initialized when the class is loaded.

    static void displayInformation(String value) {
        System.out.println("Information Display: "+value);
    }

We have written a static method which takes String as parameter and prints a string.

Actual Code:

public class StaticExample {

    private static String value1 = "value";
    protected static String value2 = "value";

    static String block;

    static void displayInformation(String value) {
        System.out.println("Information Display: "+value);
    }
}

Now in our main method, Let us access the static method

    public static void main(String args[]) {

        StaticExample.displayInformation(StaticExample.value1);
}

This should print – Information Display: Value.

Can Static Methods be Overloaded?

Yes, Static methods can be overloaded.

Example:

Static Methods Overloading

Can Static Methods be Overridden?

No, Static methods cannot be overridden. Because static methods are not associated with objects, For overriding we need objects to decide which method to be called at runtime.

Static method overriding

Can static methods have local variables?

Yes, Static methods can have local variables similar to normal functions.

    static void displayInformation(String value) {
        String valueAdded = value + " Added"; //static method local variable
        System.out.println("Information Display: "+valueAdded);
    }

Can we use non-static variables within static method?

No, It is not possible to access non-static field within static method.

Non-Static Variables within Static Method

Can we declare a Static variable within Static Method?

Let us give a try,

Static variable within Static method

Seems it is not possible! 🙂

Accessing non-static members are restricted within static method.

Can we access one Static Method within Another Static Method?

yes, let us try it out!

Static Method within Static Method

Modifying Static variable values:

Modifying Static Variable Values

Static Class:

We can have static class within our class. Let us see how can we create and access a static class.

Static Class

We have created a static class with a local variable block1 and a method innerDisplay.

Now let us see how to call this static class,

    public static void main(String args[]) {

        StaticExample.displayInformation(StaticExample.value1);

        StaticExample.Inner.innerDisplay();
}

We can access by ClassName.StaticClassName.methodName.

Can outer class be Static?

Outer class cannot be static as per the Java Language Specifications, Refer here.

Can we have nested static class?

Yes, we can have nested static class. Please find the below example,

Static Inner Class

To Access it,

Accessing Static Nested Class

Can we have non-static methods in Static Class?

There is no use of having non-static methods within static class, as we cannot create object for Static class and without object we will not be able to access the non-static methods.

Can Static Methods or Classes be Inherited?

Yes, we can inherit Static methods or classes or variables.

We can also create a same static method with same parameter as of parent class, But that does not mean it is overriding. Static methods cannot be overridden.

Actually, We are simply hiding the parent class method.

For example let us inherit the InnerClass which we have used above,

Inheriting Static Class

Static Block

Static block is used to initializing static variables.

Here is an example of static block,

Static Block

We can initialize static variables in static block but we cannot declare static variables within static block

We can create local member variables within static block

What is the use of static block and how it is different that static variables and methods?

As mentioned above, static block is used for initializing static members. But a normal declaration will do that how is the static block exceptional?

Consider this scenario,

We have a static method and we would like to execute the method when the class loads.

Let us try to understand with an example,

Consider we have a static variable but it is not initialized.

private static String value1;

Now we have a method which initializes this variable and performs some operation,

    static void display() {
        value1 = "New Value";
        System.out.println("This is a static method: " + value1);
    }

Now until we call ClassName.display(), the variable value1 will not be initialized and it will be null.

how can we execute this method when the class loads instead of waiting till we call from ClassName?

Static block comes to our rescue,

    static void display() {
        value1 = "New Value";
        System.out.println("This is a static method: " + value1);
    }

    static {
        display();
        System.out.println("From static block: "+value1);
    }

We can call the static method within static block and the static method will get executed.

The complete code of above example,

public class StaticExample {

    private static String value1;


    static void display() {
        value1 = "New Value";
        System.out.println("This is a static method: " + value1);
    }

    static {
        display();
        System.out.println("From static block: "+value1);
    }

    public static void main(String args[]) {


    }

}


We have an empty main method, but when we execute this code we will get the below output,

Static Block Output

This is one of the advantages of static block.

We cannot have static blocks within a static method.

We cannot create any method in static block but can call other static methods.

That’s it for static keyword in Java, Incase if anything missed anything please do comment!

By Sri

Leave a Reply

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