When to use Static Methods in Java?

0 votes
I am a beginner and I want to know, when to use static methods.
Apr 27, 2018 in Java by Daisy
• 8,040 points
78 views

2 answers to this question.

0 votes

Static is a non-access modifier used in java, applicable for methods, variables, class. When the static keyword is used to declare any parameter, then memory is allocated only once for that parameter. 

class Employee{  

     int id;  

     String name;  

     static String company = "WIT";  

       

     static void change(){  

     company = "SRT";  

     }  

  

     Employee(int i, String n){  

     id = r;  

     name = n;  

     }  

  

     void display (){System.out.println(id+" "+name+" "+company);}  

  

    public static void main(String args[]){  

    Employee.change();  

  

    Employee e1 = new Employee (111,"John");  

    Employee e2 = new Employee (123,"Alex");  

    e1.display();  

    e2.display();  

    }  
}  


Output of this code will be:

111 John SRT
123 Alex SRT
answered Apr 27, 2018 by Akrati
• 3,150 points
0 votes

A static method has two main purposes:

  1. For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.
  2. For the state that is shared by all instances of the class, like a counter. All instance must share the same state. Methods that merely use that state should be static as well.
answered Aug 9, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,840 points
104 views
0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,840 points
80 views
0 votes
1 answer

Can Static methods be inherited in java?

A subclass inherits all of the public ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,040 points
40 views
0 votes
1 answer

How to use pairs or 2-tuples in java?

You can refer the following code: public class ...READ MORE

answered Jun 7, 2018 in Java by Daisy
• 8,040 points
40 views
0 votes
2 answers

“Could not find or load main class” mean?

Use the final modifier to enforce good initialization. Avoid returning ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,840 points
1,390 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,040 points
305 views
0 votes
1 answer

How do I replace character from string at specific indexes?

String are immutable in Java. You can't ...READ MORE

answered Apr 26, 2018 in Java by Rishabh
• 3,540 points
31 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,320 points
262 views
0 votes
1 answer

What is the use of @Override annotation in Java ? When do we use it ?

I think it is most useful as ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,150 points
70 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
37 views