This write-up, will provide information about
- What is a static method in Java?
- How to create/initialize a static method in Java?
- Difference between simple and static methods
Static method in Java
In Java, a method that does not need an object to be accessed. These methods belong to a class, not an object. We can call or access these methods by the class name directly. In order to create a static method, we just need to use a static keyword with a method name.
Static methods are used for memory management which means these methods do not allocate memory just like objects which eventually makes the program execution faster. Following is a syntax and some rules for creating a static method in Java.
Syntax:
{
//method body
}
According to the syntax, use a static keyword to create a static method. Then specify the method return-type i.e void, int, boolean, double, long, char, and short. Lastly, specify the method name and write some code in its body.
Rules for static methods
Following are some rules for using and creating static methods.
- Static methods can be created by using a “static” keyword.
- We don’t need to create an object to access a static method
- Static methods can only access static methods or variables.
- We can not use ”this” and “super” keywords with the static method
How to create/initialize a static method in Java?
To create a static method in Java use the static keyword. The following code will help you to learn about creating and initializing static methods in Java.
Code:
public class stTest {
static void see()
{
System.out.println("This is a static method");
}
public static void main(String[] args)
{
stTest.see();
}
}
In this code, we create a class stTest with a static method see(). Then is the main method we directly call the see() method by the class.
Output:
The output shows that we call a static method directly without creating its object.
Difference between simple(non-static) and static methods
Now to understand the static methods in a better way. Let’s look at the following example which compares simple and static methods.
Code:
public void sub()
{
int g, h, res;
g=2; h=3;
res = g-h;
System.out.println("Answer = "+res);
}
public static void add()
{
int k, m, res;
k=2; m=3;
res = k+m;
System.out.println("Answer = "+res);
}
public static void main(String[] args)
{
stTest get = new stTest();
get.sub();
stTest.add();
}
}
In this code, we create two methods. The first one is sub() which is a non-static method and the second one is add() which is a static method within the stTest class. Then we create an object for the stTest class to access its non-static method.
Output:
The output shows that we need a class object to access a non-static(simple) method whereas to access the static method we just call it directly by the class name which means we do not need any object to access the static method.
Conclusion:
In Java, a static method is a method that does not need any object or instance of a class for accessing. We can call static methods directly by the class name. In this article, we have talked about static methods and some rules to use and create a static method in Java. Additionally, we have also provided differences between static and simple methods in Java.