What is a Static Method & Static Keyword in Java?

Introduction

Java is also considered as an extended version of c++, and java has removed a few of the new features and added a few unique features. We don’t need to do any work for clearing space for unreferenced objects because garbage collector in java does the hard work for you.

Java also has few cool features like platform independence, architecture-neutral, portability, multi-threaded execution, etc.

And the one feature which attracts the most is the static keyword.

So what is a static keyword?

Static Keyword in Java

Can be prefixed static keyword to a class, method, block, variable. And when a member prefixed with a static keyword, only one instance of that member will be created, and all the class cases will share the same model. People who are familiar with operating systems can assume it as a shared resource between processes.

Before we dive into static methods, let’s walk through static variables explanation.

When a variable is declared as static, then only one instance of that variable is created. Even though a class may have multiple causes, but only a single copy of that variable. And the static variables are stored in metaspace which is a pool space in JVM. And another interesting point about static members is, we don’t need to create an object to access these members. Can directly access the class by referring to their variable name.

public class upGrad

{

    public static int key=0;

    upGrad(){

        key++;

    }

public static void main(String[] args) {

System.out.println(key); //line 1

upGrad u1=new upGrad();

System.out.println(u1.key); //line 2

upGrad u2=new upGrad();

System.out.println(u2.key); //line 3

System.out.println(u1.key); //line 4

}

}

In the above code, we’ve declared a variable ‘key’ and prefixed it with the static keyword. Now coming to the primary method, line 1 will not throw any error because we can access the static members without creating an instance of the class. And in the constructor, we are incrementing the critical variable.

We can see that an example of the class upGrad has been created. It’s completely ok if we access a static variable with the class created—line 2 prints two since the variable is incremented in the constructor of the object u1. And line 3 also increments the variable, and it prints three as the output since we are creating an object u2. 

If we access the static variable using the object u1, the value will be 3, but not two because only a single instance of the member is created. It is independent of the number of objects created and which model is being used to access the variable.

Now coming to our topic, let’s discuss the static method in java.

Related: Java Architecture & Components Explained

Static Method in Java

Like static fields in java, static methods are created only once for a class, instead of an individual copy for each study’s instance. Also, we can call a static method without creating an instance of the class.

public class upGrad

{

    public static int square(int i){

        return i*i;

    }

public static void main(String[] args) {

int number=2;

upGrad u1=new upGrad();

System.out.println(u1.square(3));

System.out.println(square(number));

}

}

In the above code, we’ve created a static method that expects the number as the parameter and returns that number’s square. Now, as shown in the primary method, we can call that method without creating an object. Again as earlier, it will not throw an error if we call it using an item.

Also, there are a few points which have to be noted.

  • Since static methods and fields are settled at compile time itself, we can’t utilize the runtime features like polymorphism. So we cannot override a static method.
  • We cannot use a non-static variable inside a static method.
  • Static methods cannot use ‘super’ and ‘this’ keywords inside their implementation.
  • A static method cannot be prefixed with the abstract keyword since it cannot override it.

An instance method can access static variables and static modes, but the vice-versa is not valid. An instance method can use instance members and static members, but a static method can access only static members but not instance members.

Also Read: Java Developer Salary in India

Conclusion

In this article, we’ve discussed java features, understood the static keyword in java. I walked through static fields in java and then discussed static methods in java, has a look over the implementations of static variables and static methods. Also, we’ve got a brief idea about do’s and don’ts about static methods.

Now that you are aware of static methods and variables try implementing your next code using these static methods. And try exploring various fields which can be prefixed with the static keyword.

If you wish to improve your Java skills, you need to get your hands on these java projects. If you’re interested to learn more about Java, full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development, which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
ENROLL NOW @ UPGRAD

Leave a comment

Your email address will not be published.

×