When to use static methods

0 votes

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static method?

Example:

Obj x = new Obj();
x.someMethod();

...or:

Obj.someMethod(); // Is this the static way?

I'm rather confused!

Dec 28, 2020 in Java by Rajiv
• 8,880 points
68 views

2 answers to this question.

0 votes

One rule-of-thumb: ask yourself "Does it make sense to call this method, even if no object has been constructed yet?" If so, it should definitely be static.

So in a class Car you might have a method:

double convertMpgToKpl(double mpg)

...which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But this method (which sets the efficiency of one particular Car):

void setMileage(double mpg)

...can't be static since it's inconceivable to call the method before any Car has been constructed.

(By the way, the converse isn't always true: you might sometimes have a method which involves two Car objects, and still want it to be static. E.g.:

Car theMoreEfficientOf(Car c1, Car c2)

Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.

answered Dec 28, 2020 by Gitika
• 65,870 points
0 votes

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class.

  • In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method.
  • If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.
  • While instantiating it has to call the constructor of that class, There will be ambiguity if the constructor of that class takes an argument.
  • Static method of a class can be called by using the class name only without creating an object of a class.
  • The main() method in Java must be declared publicstatic and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.

    Example

    class Book {
       public static void getBookInfo() { //static method
          System.out.println("Welcome to Edureka");
       }
    }
    public class Test {
       public static void main(String[] args) {
          //Call static method of Book class using class name only
          Book.getBookInfo();
       }
    }

    Output

    Welcome to Edureka
answered Dec 28, 2020 by Reshma

Related Questions In Java

0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,220 points
1,013 views
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,890 points
307 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,890 points
544 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,110 points
903 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,680 points
911 views
0 votes
1 answer

Overriding private or static method in Java

You cannot override a private or static ...READ MORE

answered Jul 30, 2018 in Java by code.reaper12
• 3,500 points
503 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,890 points
2,777 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,110 points
1,837 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,870 points
122 views
0 votes
1 answer

Implements vs extends: When to use? What's the difference?

Extends is for extending a class. implements are for implementing an interface The difference between ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,870 points
73 views