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,120 points
15,511 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

Hope it helps!!

If not then its recommended to join our Java training class and learn about Java in detail.

Thank You!!

answered Apr 27, 2018 by Akrati
• 3,190 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 10, 2018 by samarth295
• 2,220 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,910 points
846 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,910 points
1,554 views
0 votes
1 answer

When to use LinkedList over ArrayList in Java?

LinkedList and ArrayList are two different implementations of the List ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,910 points
1,641 views
0 votes
2 answers

When to use static methods

Java main() method is always static, so that compiler ...READ MORE

answered Dec 29, 2020 in Java by Reshma
721 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,910 points
4,130 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,120 points
3,743 views
0 votes
2 answers

How do I replace character from string at specific indexes?

You could turn the String into a ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
22,197 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 9, 2018 in Java by geek.erkami
• 2,680 points
1,874 views
0 votes
2 answers

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

@Override annotation is used when we override ...READ MORE

answered Aug 14, 2019 in Java by Sirajul
• 59,230 points
3,054 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,220 points
948 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP