What is the function of a static method?

Asked

Viewed 25,089 times

39

Is it just convenience? I mean, it’s unnecessary to instantiate an object to use a function that doesn’t use the data from it.

Is there a difference in execution? Memory, processing - resources in general?

  • Static method does not need the object to be accessed, it is one of the functions if I am not mistaken...

3 answers

44


Instance X types

In a certain way we can say that it is impossible to instantiate an object to access a static method. Static methods never operate in the instance. If they’re in static classes it wouldn’t even make sense. If the static method is in a normal class or structure, it cannot access instance members (at least in a privileged way). Why: it would access which instance?

Static methods can only access static state since they are valid for the entire application only to access data that is also valid for the entire application. Static states exist in the class/structure. Of course instances can access them referenced to the type and not the instance.

So you don’t need to instantiate anything to access static methods. And methods that don’t use instance data, in general, should be static. Purists don’t like it, they usually have a fit when they find a static method even when they make sense. The "rules" they invented or copied say that static methods are the devil’s thing. Good thing I don’t follow the "rules", I solve problems. Mine default is to create static methods and only create instance method when it is useful and necessary (the detail is that it has few deafults in my code, which is good, makes me think). But I understand that this does not work for everyone.

Unlike Java, you cannot call the static method using the instance. Java allows and instead of accessing the instance the compiler translates the call to a class call. In C# the programmer has to call the static method through its type and there is no way to call through the instance even if it was ignored. Interestingly the extension methods allow to give appearance to do this, even if they still function as static methods.

The static method can almost be considered a distortion for those who know other languages. In the background they are common functions and the AP used the term well.

Function of static methods

To understand its function (without puns) it is good to understand the difference between it and the instance method.

An instance method has an extra parameter that is hidden and this parameter gives access to the state of the object in question, so it is always working with a specific object. Essentially this is what differs one method from the other. Of course, the instance method can have polymorphism and other characteristics that only make sense with instances. This method may have privileged access to members.

This extra parameter determines that the method must operate directly with the object, the instance (nothing prevents the instance method from accessing any class member). There are currents that say this is preferred because it is more object oriented and the method is already present in the API contract. I already prefer to make static everything that does not access an instance. Each has its merits.

In a static method all parameters are treated identically, there are no access privileges to members of a parameter. And obviously it cannot have any characteristic related to instances, such as polymorphism (it has solutions for this). We can understand the static method as a utility method, as something that does not belong to the object and is there only to be encapsulated in the type.

It is customary to give preference to static methods that do not access static state since global state may be of the devil. That is, conceptually static methods tend to be algorithmic and do not access anything beyond the parameters - even if it is possible to access static data or, obviously, create local instances of various types.

Differences in internal implementation

Apart from these small conceptual differences, there is not much that differs internally. When they see native code, you can barely tell which one is which. They perform equally, they alone in essence consume the same memory, processing or anything.

Just remembering that an instance method has one more parameter than you see. But you can’t say that it consumes anything else relevant. And even consuming, to do exactly the same operation the code needs an extra explicit parameter in the static method compared to the instance method.

public static void metodo1(Conta conta, int valor) { ... }

public void metodo2(int valor) { ... } //pertence à classe Conta, é de instância

public static void metodo3(int valor) { ... }

I put in the Github for future reference.

These first two methods perform and consume the same thing. The compiler can even treat them differently initially but then it doesn’t make much difference. The third is not the same as the first two independent of its code.

Static data tend to consume slightly less memory than instantiated data, in addition to having special area allocation, but it is derisory. They do not differ in their essence.

Completion

Many people do not know but it is possible to do everything with static methods. This website What you’re using now is done with a lot of static stuff. Because the way it is used can bring simplifications and give some performance advantage, but not because they are static. The object orientation paradigm brings some cost. Some programmers can understand the whole model better with static method. This may sound strange to some programmers, but it’s true.

The differences in their implementation and use are conceptual, help facilitate coding but do not differ in internal functioning. If they are used in an equivalent way, it makes no difference to use one or the other from an implementation point of view. The choice must be made by semantic necessity and not by how they work internally.

Documentation.

24

What is the function of a static method?

Execute a method without needing to instantiate a class. A class, roughly speaking, is a set of variables, properties, and methods. When the first two are not needed in an application context (that is, when all internal components as variables and properties are private, or when it is not necessary to conserve an object state, with those variables and properties with certain values), the class can be static.

Is it just convenience? I mean, it’s unnecessary to instantiate an object to use a function that doesn’t use the data from it?

Not just convenience. It has certain application memory saving as well.

Is there a difference in execution? Memory, processing - resources in general?

Yes, the static method is slightly more efficient, but the difference is almost imperceptible. There are more details in this great OS response.

6

There is a difference, because the execution of this method does not participate exclusively in the context of who is calling the method but rather of application or better Appdomain. Static methods are very useful for algorithms that do not need shared resources as instance properties.

Browser other questions tagged

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