What is Static Method in Java with Examples

static method

You may have heard the term “static method” before, but what does it mean? 

In Java, a static method is a method that belongs to the class, rather than an instance of the class. Static methods are accessed through the class name, rather than an object of the class. In this article, we’ll discuss what static methods are, when you should use them, and how to call them. We’ll also take a look at some of the restrictions of static methods. We’ll go over everything you need to know about static methods in Java.

  1. Static Method in Java
  2. Java Static Method Example
  3. Benefits of Static Methods
  4. Drawbacks of Static Methods
  5. When Should We Use Static Methods?
  6. Rules for Static Methods
  7. How to Call a Static Method From Another Class? 
  8. Static Method vs. Instance Method
  9. What is the Difference Between a Static and an Instance Method?
  10. Common Uses of Static Methods
  11. How to Call a Non-Static Method From Main in Java? 
  12. Memory Allocation of Static Methods 
  13. Conclusion

Static Method in Java 

What is a static method in Java? 

Static methods are methods that are associated with a class rather than an object. They are declared using the keyword static. 

We can call static methods without creating an instance of the class. 

Static methods are often used to perform operations, not specific to any particular object, such as mathematical operations or checking the type of an object. Before moving ahead, you can also take up a free online Java Programming course to gain a clear understanding of the basic concepts.

Java Static Method Example: 

package com.java.demo; 
class Student 
{ 
int stdId; 
String stdName; 
static String collegeName = "CIT"; 
//The following static method , valueChange to change the value of static variable static void valueChange() 
{ 
collegeName = "MIT"; 
} 
//The following constructor is used to initialize the variable 
Student(int id, String name){ 
stdId = id; 
stdName = name; 
} 
//The display method to display values 
void display() 
{ 
System.out.println(stdId+"  "+stdName+"  "+collegeName); 
} 
} 
//The class Demo is used to create and display the values of the object 
public class Demo 
{ 
public static void main(String args[]) 
{ 
Student.valueChange();//calling valueChange method 
//creating objects 
Student StudentObj = new Student(211,"Kamal"); 
Student StudentObj1 = new Student(125,"Shravan"); 
Student StudentObj2 = new Student(321,”Michael"); 
//calling display method 
StudentObj.display(); 
StudentObj1.display(); 
StudentObj2.display(); 
} 
} 

Output: 

211 Kamal MIT

125 Shravan MIT

321 Michael MIT

Benefits of Static Methods 

The main advantage is that they are more efficient than non-static methods since the compiler can inline the code of a static method into the caller. They can also be accessed from anywhere in your code without having to create an instance of the class. 

Also Read: Java Tutorial for Beginners

Drawbacks of Static Methods 

One of the drawbacks is that they cannot be called from the instance method outside of the class in which they are defined. So, if you want to use a static method, you have to call it from another static method itself. This is a general rule for static keywords in java that static things, may it be methods or variables, cannot be referred to from the non-static area. 

Another downside is that they can create hidden dependencies between classes. This means that if Class A calls Class B’s static method, and Class B’s method depends on data from Class C, then Classes A and B are now indirectly linked. If Class C is changed in any way, Classes A and B will need to be updated as well. 

The final drawback of static methods is that we cannot override static methods. 

When Should We Use Static Methods? 

So, when is it appropriate to use a static method? Generally speaking, they are helpful for operations not dependent on any particular class instance, such as mathematical calculations or checking for equality. 

Rules for Static Methods 

They are particular methods that belong to a class rather than an instance of a class. The static keyword precedes them, and they must be called using the name of the class rather than an instance of the class. 

A few rules you need to follow: 

  • Static methods cannot access a class’s non-static members (variables or methods). 
  • Static methods cannot be overridden. 
  • We cannot use the “this” keyword inside the static method.

The main benefit of static methods is that they are more efficient than standard methods. They don’t require an instance of a class to run, so they’re perfect for tasks that don’t depend on any specific instance data. However, there are a few drawbacks to using static methods: they can make your code less flexible, and they can’t be overridden if you need to add custom functionality later on. 

How to Call a Static Method From Another Class? 

To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName.methodName(). 

Static Method vs. Instance Method 

Static Methods  Instance Methods
Can be called without the objects, Require an object creation to call an instance method
Associated with the class  Associated with the objects
Can access only static attributes. Can access all attributes of a class
Requires to be declared using static keyword  Doesn’t require any keyword

What is the Difference Between a Static and an Instance Method? 

Static methods are a type of method that belongs to a class rather than an instance. This means that they can be called without creating an instance of the class and can only be called from within the same package or from a subclass. 

Static methods are often used for utility functions, such as mathematical operations or input/output operations, whereas instance methods are used for actions that are specific to a particular object. 

Instance methods can be overridden in subclasses, but static methods cannot. Hence, static methods are a part of Java’s class-based design and allow you to call methods without creating an instance of the class. You can call a static method from another static method, but you can’t call a static method from an instance method. 

Common Uses of Static Methods 

They are commonly used for utility functions, such as checking input data or converting data from one format to another. They can also be used for initialization tasks that need to be performed only once, such as initializing static variables. 

How to Call a Non-Static Method From Main in Java? 

To call a non-static method from main in Java, you first need to create an instance of the class, post which we can call using objectName.methodName(). 

Difference Between Static and Non-Static Variables 

There is a big difference between static and non-static variables. Static variables are class-level variables, whereas non-static variables are instance-level variables. Static methods can be called without creating an instance of the class, whereas non-static methods cannot. Static methods can only be called directly (without using the class name) from within the class they are declared, whereas non-static methods can be called from other classes. 

Also Read: Top 200+ Java Interview Questions and Answers 2022

When Should We Use Static Methods? 

They should be used when you want to do something that is not specific to any particular class instance. For example, you might want to write a method that prints the contents of a file. This method does not need to know anything about the particular file being printed, so it can be declared static. 

What if the Static Variable Refers to an Object? 

When you use a static method, you refer to the class itself rather than any specific class instance. This means that it can be called from anywhere, regardless of whether an object has been created or not. 

For example, let’s say you have a class called Employee, and you want to create a static method to calculate the employee’s hourly wage. You could write the following code: 

public static double calculateWage(Employee employee) { // code to calculate wage goes here}. This method can then be called from anywhere without needing an instance of the Employee class. 

How to Call Non-Static Methods From Static Methods? 

However, you can only call non-static methods from a static method if the non-static method is declared as “static”. Else, it throws a compilation error. 

The rules of static methods in Java are pretty straightforward, but it’s essential to understand how they work to make the most of them. By using static methods effectively, you can make your code more organized and efficient. 

Memory Allocation of Static Methods 

Static methods is a particular type of method that belongs to the class rather than any specific class instance. This means you can call a static method from anywhere in your code without creating an instance of the class first.

Static methods are always called in the same way, regardless of the object used. This is because they are stored in a particular memory section allocated when the class is loaded. This means that they don’t use up any memory allocated to individual objects, and they don’t have to be reinitialized every time they are called. 

Static methods differ from instance methods, which are only available when you create an instance of the class. Instance methods can only be called from within the object that contains them, and they use up part of the object’s memory allocation.  

Conclusion

We have learned what static methods are and when they should be used. We now understand the difference between static and non-static variables and know how to call a static method from another class. You also know the restrictions of the same. With this knowledge, one can now confidently use static methods in your Java programs. 

We now know all there is to know about static methods in Java. We know when and how to use them and the benefits they offer. So, what are you waiting for? Start using static methods in your Java programs today! 

Leave a Reply

Your email address will not be published.