Are static methods good for scalability?

后端 未结 8 534
故里飘歌
故里飘歌 2021-02-04 19:03

Does static methods and class are good for scalability ? I think so static class/method improves scalability of application and instance methods doesn\'t scales much. So is it g

8条回答
  •  清酒与你
    2021-02-04 19:21

    No. I think you may be assuming that each instance has its own copy of the method definition, taking up that amount of space for each instance, which is not the case.

    Editing to add:

    In case you wonder how an instance method can be actually shared between the instances: It is because each call to an instance method implicity passes a reference to the instance object to the method. This is generally referred to as the "implicit this" In other words - when you define or call an instance method with two parameters like myMethod(a, b), you can think of it as actually being myMethod(this, a, b), and Java takes care of the this parameter for you, without your having to explicitly define or pass it.

    (This, by the way, is handled differently in Python, where you have to explicitly put the object reference in as the first parameter of an instance method definition, though not in the call.)

    For an explanation of what goes on at the Java bytecode level here's a link: http://www.artima.com/underthehood/invocationP.html (See stuff around: "The objectref is the implicit this pointer that is passed to any instance method.")

提交回复
热议问题