Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconWhat is a Static Method & Static Keyword in Java?

What is a Static Method & Static Keyword in Java?

Last updated:
23rd Feb, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
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.

Ads of upGrad blog

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

Ads of upGrad blog

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What 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.

2What 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.

3What 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.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Full Stack Developer Salary in India in 2024 [For Freshers & Experienced]
907173
Wondering what is the range of Full Stack Developer salary in India? Choosing a career in the tech sector can be tricky. You wouldn’t want to choose
Read More

by Rohan Vats

15 Jul 2024

SQL Developer Salary in India 2024 [For Freshers & Experienced]
902296
They use high-traffic websites for banks and IRCTC without realizing that thousands of queries raised simultaneously from different locations are hand
Read More

by Rohan Vats

15 Jul 2024

Library Management System Project in Java [Comprehensive Guide]
46958
Library Management Systems are a great way to monitor books, add them, update information in it, search for the suitable one, issue it, and return it
Read More

by Rohan Vats

15 Jul 2024

Bitwise Operators in C [With Coding Examples]
51783
Introduction Operators are essential components of every programming language. They are the symbols that are used to achieve certain logical, mathema
Read More

by Rohan Vats

15 Jul 2024

ReactJS Developer Salary in India in 2024 [For Freshers & Experienced]
902674
Hey! So you want to become a React.JS Developer?  The world has seen an enormous rise in the growth of frontend web technologies, which includes web a
Read More

by Rohan Vats

15 Jul 2024

Password Validation in JavaScript [Step by Step Setup Explained]
48976
Any website that supports authentication and authorization always asks for a username and password through login. If not so, the user needs to registe
Read More

by Rohan Vats

13 Jul 2024

Memory Allocation in Java: Everything You Need To Know in 2024-25
74112
Starting with Java development, it’s essential to understand memory allocation in Java for optimizing application performance and ensuring effic
Read More

by Rohan Vats

12 Jul 2024

Selenium Projects with Eclipse Samples in 2024
43493
Selenium is among the prominent technologies in the automation section of web testing. By using Selenium properly, you can make your testing process q
Read More

by Rohan Vats

10 Jul 2024

Top 10 Skills to Become a Full-Stack Developer in 2024
229999
In the modern world, if we talk about professional versatility, there’s no one better than a Full Stack Developer to represent the term “versatile.” W
Read More

by Rohan Vats

10 Jul 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon