Programs

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.

Check out our free courses to get an edge over the competition.

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.

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

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

}

}

Check out upGrad’s Java Bootcamp.  

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.

Explore Our Software Development Free Courses

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

Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

In-Demand Software Development Skills

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 Executive PG Program 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.

What is a static method in Java?

A static method is one which can be called without creating an instance of the class. Suppose you have a class named Car, then you can call its static methods as Car.methodname(). If you call non-static methods in the class, then you need to first create an instance of the class. Static methods usually have little use in Java. They are used to define one-time utility functions that can be accessed from the class directly rather than from object references. Static methods are not associated with objects, so unlike other methods they cannot refer to an instance variable of the class.

What are static variables in Java?

A static variable is a variable that is created by the class itself and can be called without creating an instance of the class. Static variables have the life time of the class, which means they are only initialized once. Also, static variables are unique to each instance of the class, which means they hold a single copy of the variable. This can be accomplished by putting static keyword in front of the variable. Static variables are useful if we want to keep track of some information that is global to the class, like counting how many objects have been created or holding the last time something was executed. They are also used to perform some action on behalf of the class, like defining some pre-conditions or post-conditions for the methods of a class.

What are the differences between static and normal methods in Java?

A static method is declared using the keyword static while a normal method is declared without using any keyword. A static method is invoked without an instance of the class. It can be invoked without having an instance of the class. Instance of the class is needed when we make use of a normal method. Static method belongs to the class not to its instances. So, it does not belong to any particular object of that class. Static methods can be invoked with or without creating an instance of the class. But normal methods can be invoked only with an instance of the class.

Want to share this article?

Become a Full Stack Developer

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks