Static Keyword in Java – Static Methods, Variables and Classes

Someone who starts programming in Java is aware of the Static Methods. Static Methods has always been a confusing topic in Java for many of the programmers.

In this article of static method, we attempt to clear all your myths and troubles related to the Static Method. Firstly one has to understand that Static is only a Keyword that is used to define the methods, blocks, classes as well as variables also.

Keywords are some reserved words that are predefined in the Java library to indicate some predefined actions. As keywords are the reserved words so we can not use it as the class name, variable name or method name.

Now, we will discuss how we can use the static keywords with the class, method, block, and variables with examples.

Let’s fall into the programming paradigm of Static Methods and try to learn every aspect of it.

Static Method v/s Instance Method

Instance Method

The method that usually every programmer uses in their program is an Instance method. The instance method clearly specifies from its name that we need to create an instance to access this method.

public void Instance_Method(String[] args) //method without static keyword
{
  System.out.println("Example of Instance method");
}

To execute the above piece of code we have to create an instance of the class then call this method. The purpose of the Instance method is “when we need we call it”.

That simply means that the Instance Method needs some mechanism to execute.

Static Method

When we talk about the Static method we have to assign a special keyword to indicate that the method is static. To declare any Static Method, we have to write a static Keyword before the method name.

Syntax of writing static method:

static <method_name>

As we discussed in the Instance method, we have to create an instance for it. In the static method, we don’t have to create an instance of the method to execute it.

public static void Static_Method(String[] args) //method with static keyword
{
  System.out.println("Example of Static method");
}

Java automatically executes the method with the static keyword. The programmer just needs to declare the static method and Java executes it.

The purpose of the static method is when we need some piece of code that has to execute every time then we declare it as Static.

Let’s take one more step towards the Static method and take a closer look at it.

Java Static Variable

In Java, the static variables are also called the class variables. If we declare any variable with the static keyword, then it is said to be a static variable.

The variables declared with the static keyword refer to the common property for all the objects of the class.

For example, the university name is the same for its students, employees, faculties, etc. In other words, we can say that only a single copy of the static variable is there which is shared among all the instances of the class.

We can access the static variables directly from static as well as non-static methods.

Code to Understand Static Variable in Java:

package com.techvidvan.statickeyword;
public class Static_Variable {
  static int number1; //Static variable type: int
  static int number2; //Static variable type: int
  static String str1; //Static variable type: String
  static String str2; //Static variable type: String

  public static void main(String[] args) {
    number1 = 101;
    number2 = 102;
    str1 = “Techvidvan”;
    str2 = “Static variable article”;
    System.out.println("Static Number: " + number1);
    System.out.println("Static Number: " + number2);
    System.out.println("Static String: " + str1);
    System.out.println("Static String: " + str2);
  }
}

Output:

Static Number: 101
Static Number: 102
Static String: Techvidvan
Static String: Static variable article

Java Static Block

We know that a block is nothing but the lines of code enclosed within curly braces. If we declare any block as static, then such blocks are called static blocks in Java.

We use the static block when we need to initialize the static variables. The static blocks load or execute at the time of loading a class. There can be multiple static blocks inside a Java program.

And these multiple static blocks always execute in a sequential manner according to which they are written in the program.

Code to understand Single Static Block in Java:

package com.techvidvan.statickeyword;
public class Single_Static_Block {
  static int number; //Static variable type: int
  static String str; //Static variable type: String

  static {
    // Static Block
    number = 13;
    str = "Static Block in Java: Techvidvan";
  }

  public static void main(String[] args) {
    System.out.println("Static Number: " + number);
    System.out.println("Static String: " + str);
  }
}

Output:

Static Number: 13
Static String: Static Block in Java: Techvidvan

Code to understand Multiple Static Blocks in Java:

package com.techvidvan.statickeyword;
public class Multiple_Static_Blocks {
  static int number; //Static variable type: int
  static String str; //Static variable type: String

  // Static Block one
  static {
    System.out.println("First Static Block:");
    number = 51;
    str = "Static Block Example: Techvidvan";
  }

  // Static Block two
  static {
    System.out.println("Second Static Block:");
    number = 10;
    str = "Block two: Techvidvan";
  }

  public static void main(String[] args) {
    System.out.println("Static Number: " + number);
    System.out.println("Static String: " + str);
  }
}

Output:

First Static Block:
Second Static Block:
Static Number: 10
Static String: Block two: Techvidvan

Java Static Methods

Static methods are the methods declared with the static keyword. When we declare a method as static, we can call this method or access this method without creating an object or instance of the class.

As we know that for calling non-static methods, we need to first create the object of the class and then call the method through the object, but unlike the non-static methods, we can call the static methods directly with the class name.

The syntax of calling static methods is:

ClassName.StaticMethodName();

Or, we can also directly call the static method like this:

StaticMethodName();

First Code to Understand Static Methods in Java:

package com.techvidvan.statickeyword;
public class Static_Method {
  static void methodStatic() // declaring a static method
  {
    int num1 = 10;
    int num2 = 20;
    int sum = num1 + num2;
    System.out.println("Static method: Techvidvan");
    System.out.println("Sum of " + num1 + " and " + num2 + " is " + sum);
  }

  public static void main(String[] args) {
    /* Here we can see that
     * we call our methods without any instance
     * this is the purpose of static method 
    */
    methodStatic();
  }
}

Output:

Static method: Techvidvan
Sum of 10 and 20 is 30

Second Code to Understand Static Methods in Java:

package com.techvidvan.statickeyword;
public class Static_Method_Two {
  // declaring static variable type: int and String
  static int number1 = 10;
  static int number2 = 5;
  static String str = "Techvidvan";

  // declaring Static Method
  static void multiply() {
    int multi = number1 * number2;
    System.out.println("The multiplication of number " + number1 + " and " + number2 + " is " + multi);
  }

  // declaring a non-static method
  void doMultiply() {
    System.out.println("Techvidvan: Static method article");

    // a non-static method can also call a static method directly
    multiply();
  }

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

    //To call a non-static method we have to create an instance and we call the method

    obj.doMultiply();

    // we call static method directly by the class name
    Static_Method_Two.multiply();
  }
}

Output:

Techvidvan: Static method article
The multiplication of number 10 and 5 is 50
The multiplication of number 10 and 5 is 50

Java Static Classes

We can also declare a class as static; the condition being that the class should be a nested class, i.e, it should be present within the class, then only we can declare it as static.

The nested static class does not require an object of its Outer class. One more restriction with the nested static class is that it cannot access the non-static data members of the Outer class.

Code to understand the Static class in Java:

package com.techvidvan.statickeyword;
public class Main {
  private static String myStr = "Techvidvan: Static Class Article";
  private static int number1 = 20;
  private static int number2 = 5;

  //declaring Static class
  static class NestedStatic {

    //declaring non-static method
    public void display() {
      int minus = number1 - number2;
      System.out.println("The Subtraction of number " + number1 + " and " + number2 + " is " + minus);
      System.out.println(myStr);
    }
  }
  public static void main(String args[]) {
    Main.NestedStatic obj = new Main.NestedStatic();
    obj.display();
  }
}

Output:

The Subtraction of number 20 and 5 is 15
Techvidvan: Static Class Article

What is the main() method of Java static?

You have used the main() method in every Java program. The main() method of Java looks like:

public static void main(String args[]) {
  //code inside the main() method
}

Have you ever wondered why this main() method is always static?

It is because there is no need to create an object of the class to call this method. We have already learned this fact in the section of the Static method of this article.

We know that during the execution of the java program, the JVM always starts with the main() method of the class, and suppose this main() method was non-static then JVM had to create the object of the class and then call this method which would raise the problem of memory allocation.

Therefore the main() method of Java is always declared as static.

Advantages of Static Keyword in Java

1. Anything declared as static can be globally accessible.

2. We need only one object per class to access all the static variables that save the memory.

3. We can call static methods directly. So, there is no need to create objects.

4. Static keywords might possibly give the performance efficiency in the code as there is no need of checking for null.

5. Static keywords can do meta operations on objects like counting the number of objects or validating the object.

Conclusion

Static keyword is one of the reserved keywords among the 57 keywords of Java that plays a very important role in efficient memory management.

We can use the static keyword with Java class, Java blocks, Java variables, and Java methods. We can only make a nested class as static in Java. If we want to initialize a static variable then we declare a block as static.

If we want to directly call the method without creating an object of the class, then use the static methods. And use the static keyword with variables when you want that only a single copy of the object is shared among all the variables.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.