-3

I am trying to invoke the static Method. What I know is that we have to use the class name to invoke a static method. However, I found that static methods can be invoked even with the help of object.

For example:

public class SwitchStaticMethod {
//static methods can also be called by object
    /**
     * @param args the command line arguments
     */

    public static int getMonthNumber(String month) {
    int monthNumber = 0; 
    if (month == null) { 
      return monthNumber; } 
    switch (month.toLowerCase()) { 
      case "january": monthNumber = 1; break; 
      case "february": monthNumber = 2; break; 
      case "march": monthNumber = 3; break; 
      case "april": monthNumber = 4; break; 
      case "may": monthNumber = 5; break; 
      case "june": monthNumber = 6; break; 
      case "july": monthNumber = 7; break; 
      case "august": monthNumber = 8; break; 
      case "september": monthNumber = 9; break; 
      case "october": monthNumber = 10; break; 
      case "november": monthNumber = 11; break; 
      case "december": monthNumber = 12; break; 
      default: monthNumber = 0; break; } 
      return monthNumber; } 

    public static void main(String[] args) {
        // TODO code application logic here
        String month = "August";

      SwitchStaticMethod objSNM = new SwitchStaticMethod();
      int returnedMonthNumber = objSNM.getMonthNumber(month); 
      if (returnedMonthNumber == 0) { 
         System.out.println("Invalid month"); 
      } else { 
         System.out.println(returnedMonthNumber); 
     }   // TODO c
    }

}

Similarly, I can invoke static method without the help of an object as in the following code:

public class SwitchStaticMethod2 {

    /**
     * @param args the command line arguments
     */
     public static int getMonthNumber(String month) {
    int monthNumber = 0; 
    if (month == null) { 
      return monthNumber; } 
    switch (month.toLowerCase()) { 
      case "january": monthNumber = 1; break; 
      case "february": monthNumber = 2; break; 
      case "march": monthNumber = 3; break; 
      case "april": monthNumber = 4; break; 
      case "may": monthNumber = 5; break; 
      case "june": monthNumber = 6; break; 
      case "july": monthNumber = 7; break; 
      case "august": monthNumber = 8; break; 
      case "september": monthNumber = 9; break; 
      case "october": monthNumber = 10; break; 
      case "november": monthNumber = 11; break; 
      case "december": monthNumber = 12; break; 
      default: monthNumber = 0; break; } 
      return monthNumber; } 
    public static void main(String[] args) {
        // TODO code application logic here

        String month = "August";

      //SwitchStaticMethod2 objSNM = new SwitchStaticMethod2();
      int returnedMonthNumber = SwitchStaticMethod2.getMonthNumber(month); 
      if (returnedMonthNumber == 0) { 
         System.out.println("Invalid month"); 
      } else { 
         System.out.println(returnedMonthNumber); 
     }   // TODO c
    }
    }

I feel this is a confusion. Both are giving same answer but which is the correct way of invoking static methods?

halfer
  • 19,824
  • 17
  • 99
  • 186
user2994783
  • 141
  • 1
  • 10
  • See https://stackoverflow.com/q/4978000/217324 – Nathan Hughes Jan 23 '19 at 04:53
  • 1
    With lots of static method in a class, you end up calling classname.methodname() and that's not what code standards suggest. Here comes the static import. You should import the static methods statically with a class and go on use them. This improves the readability and the enforce standards too. – Kumar Ashutosh Jan 23 '19 at 04:59
  • Both ways are correct, but notice the difference. If you don't have an instance, do not create one (via new, its useless with impact over performance) just use class name. Java have build in classes that have static methods and you cannot create instances via new. eg. with Math. Cannot do Math m = new Math() (and after m.random()), but you can get Math.random() – Traian GEICU Jan 23 '19 at 05:23
  • Thanks. I like your answer. – user2994783 Jan 23 '19 at 14:42
  • Thanks for the link. But I can't understand how it is creating the concept of 'static' without using the static keyword. – user2994783 Jan 24 '19 at 01:26

2 Answers2

-1

It is better to call the static method using the class, just like you did in the second version.

They will both work the same way, but calling a static method from the object will have no gain, since they can't be inherited. It will only cause confusion in the end.

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
  • You can just call `getMonthNumber(month)` because you are already in the same scope as the static method. – Jason Jan 23 '19 at 04:56
-1

It is always better to invoke static methods by the class name. Why to unnecessarily create an object if your requirement is fulfilled without creating an object.

uKum14
  • 1
  • 2
  • Unrelated: your recent question collected downvotes quickly because it was fully off topic here. If you had asked "what are interview questions", people would have told you "that is not what this community is about". So "here are interview questions" is not only: not a question, even if it would be an answer on a question ... fully off topic. We document technical aspects of programming problems here, not interview questions! – GhostCat Jul 05 '19 at 10:33