Is calling static methods via an object "bad form"? Why?

how to call static method in java
static method in java with example
how to call a static method in java from another class
can we call static method with object in java
static methods c#
can you call a static method from an instance c++
when to use static methods
can we call static method with object in c++

In a recent question, someone asked about static methods and one of the answers stated that you generally call them with something like:

MyClassName.myStaticMethod();

The comments on that also stated that you could also call it via an object with:

MyClassName myVar;
myVar.myStaticMethod();

but that it was considered bad form.

Now it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not (a).

Is there some problem with calling static functions via an object? Obviously you wouldn't want to create a brand new object just to call it:

Integer xyzzy;
int plugh = xyzzy.parseInt ("42", 10);

But, if you already have an object of the desired type, is there a problem in using it?


(a) Obviously, I can't call a non-static method with:

MyClassName.myNonStaticMethod();

but that's not the issue I'm asking about here.

The bad form comment comes from the Coding Conventions for Java

See http://www.oracle.com/technetwork/java/codeconventions-137265.html#587

The reason for it, if you think about it, is that the method, being static, does not belong to any particular object. Because it belongs to the class, why would you want to elevate a particular object to such a special status that it appears to own a method?

In your particular example, you can use an existing integer through which to call parseInt (that is, it is legal in Java) but that puts the reader's focus on that particular integer object. It can be confusing to readers, and therefore the guidance is to avoid this style.

Regarding this making life easier for you the programmer, turn it around and ask what makes life easier on the reader? There are two kinds of methods: instance and static. When you see a the expression C.m and you know C is a class, you know m must be a static method. When you see x.m (where x is an instance) you can't tell, but it looks like an instance method and so most everyone reserves this syntax for instance methods only.

Is calling static methods via an object "bad form"? Why?, // Accessing the static method geek() by using Object's reference. Geek obj = new Geek();. obj.geek( "mohit  Yes you can call static methods via object also but do remember that data is still associated with class not object. Yes you can because a static variable has the same visibility as the class itself. Anywhere where the class name can be used, so can the static method name.

In my opinion, the real use case that makes this so unreadable is something like this. What does the code below print?

//in a main method somewhere
Super instance = new Sub();
instance.method();

//...
public class Super {
    public static void method() {
        System.out.println("Super");
    }
}

public class Sub extends Super {
    public static void method() {
        System.out.println("Sub");
    }
}

The answer is that it prints "Super", because static methods are not virtual. Even though instance is-a Sub, the compiler can only go on the type of the variable which is Super. However, by calling the method via instance rather than Super, you are subtly implying that it will be virtual.

In fact, a developer reading the instance.method() would have to look at the declaration of the method (its signature) to know which method it actually being called. You mention

it seems to me that doing this can actually make my life easier so I don't have to worry about what's static or not

But in the case above context is actually very important!

I can fairly confidently say it was a mistake for the language designers to allow this in the first place. Stay away.

Static methods vs Instance methods in Java, When we invoke a method on a class, we send this message to the actual object representing that class. In Java, calling a static method does not involve objects  Calling an instance method requires the creation of an object of its class, while a static method doesn't require it.

It's a matter of communication. Calling a method on an instance implies you're acting on/with that particular instance, not on/with the instance's class.

Calling static method from instance of class, Java is a Object Oriented Programming(OOP) language, which means we need objects to access How to call a non-static method: using object of the class: Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class. public static void geek (String name) { // code to be executed. } // Must have static modifier in their declaration.

It might get super confusing when you have an object that inherits from another object, overriding its static method. Especially if you're referring to the object as a type of its ancestor class. It wouldn't be obvious as to which method you're running.

The difference between regular (non-static) and static methods, In the main method, change how you call the static methods by using just the classname instead of creating an object. Add a static variable to the class that keeps  Static Variables and Static Methods Each object has its own set of member variables and all the member variables have a scope. If we want a variable to have the same value throughout all instances of the object then we can declare it as a static variable in our program.

5.7. Static Variables and Methods, The static keyword defines a static method for a class. These are often utility functions, such as functions to create or clone objects. Using static in classes You need to call them using the class name: CLASSNAME. The value of this in User.staticMethod () call is the class constructor User itself (the “object before dot” rule). Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it. For instance, we have Article objects and need a function to compare them.

static, Qualifying a static call. From outside the defining class, an instance method is called by prefixing it with an object, which is then passed as an implicit parameter   Static Methods. The third method, MyClass.staticmethod was marked with a @staticmethod decorator to flag it as a static method. This type of method takes neither a self nor a cls parameter (but of course it’s free to accept an arbitrary number of other parameters). Therefore a static method can neither modify object state nor class state.

Java: Static/Class methods, In PHP 5, calling non-static methods statically generates an E_STRICT level warning. Static properties cannot be accessed through the object using the arrow  In static method, The memory of a static method is fixed in the ram, for this reason we don’t need the object of a class in which the static method is defined to call the static method. To call the method we need to write the name of the method followed by the class name. class GFG { public static void geek () { } } // calling GFG.geek ();

Comments
  • See this SO answer for info stackoverflow.com/questions/610458/…
  • When I read code and there is a call to a method, to me it's highly relevant to be able to tell right away that that call cannot read nor modify the state of the object it belongs to. With MyClassName.myStaticMethod(); there isn't even the need to wonder about it for a second. With myVar.myStaticMethod(); you need to see the method's code, and you will always be in this doubt for all calls. Of course different IDEs might make this information easy to retrieve (Javadoc tooltips, syntax highlighting etc.) but I wouldn't rely on that to decide how to write the code.
  • +1 Agreed that it was a mistake as well (IMHO). And a nice example BTW.
  • If that "super confusing" was a pun, I'm going to have to come over and slap you around a bit :-)