Call method by class or by instance?

Asked

Viewed 248 times

1

What would be the most correct way to call a method of another class?

It is more correct to create the object:

private MinhaClasse minhaclasse;
minhaclasse = new MinhaClasse();

To then call a method:

minhaclasse.meumetodo();

Or simply call from the class:

MinhaClasse.meumetodo();

Which is more advantageous? Which is less expensive and less difficult for the system to run?

2 answers

4


private MinhaClasse minhaclasse;
minhaclasse = new MinhaClasse();

I put in the Github for future reference.

This code makes no sense, if it is inside a method the first line does not compile, if it is outside a method the second line does not compile.

I imagine you’re talking about a static method, because if it’s an instance, the first option is your only option.

For a static method does not matter, both work the same.

However, it is considered a little confusing to call a static method as if it were an instance, so the second option would be the most suitable to give more readability and prevent any error from happening by trying to access a null instance in something that should always work. So it’s not recommended.

Imagine you call a method by the instance that does something that you assume will happen with the instance, but because it is a static method it occurs globally. It’ll take a while to figure out why you don’t do what you expect.

C# chose not to allow the first option to avoid confusion.

If your concern is whether you should create the method as static or not, I’m one of those who thinks you should always create a static method until it needs to be instance, and often is. The most common is that members of a class need to be bound to the instance.

And my choice for the method is not even performance, which is even faster, is to avoid exposing something that is not necessary. If you do not depend on anything that is in the instance why do that method be instance? Of course you need to ask yourself why it has no relation to the instance. It may have been a wrong decision.

  • Take a look at the comments of the answer accepted: https://stackoverflow.com/questions/49582560/oop-in-java-android/49582605#49582605 I asked the same question, but as I understand, the guy recommends not to use unless it is strictly necessary static methods, now I’m in a real doubt

  • 1

    That’s why I said the opinion, there are those who think differently. You have to prefer the complexity of managing instance where it is not necessary. In general the argument is to facilitate the test, which is often not done, at least with that code and one instance ends up being created throughout the application life cycle just because they decided wrong. Java complicates a bit, but it is possible to have static methods and test without problems, lack creativity or find it easier to complicate real code instead of complicating the test.

2

The correct form depends on the method you call:

  • If the method has the modifier static, use the class name.

  • If the method nay has the modifier static, use the variable with the reference to the object.

There is no significant difference in performance or difficulty in performing. The focus here is that each of them serves a purpose and depends heavily on how you are organizing your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.