why private static methods exists those are not being called in any static context?

末鹿安然 提交于 2019-12-04 16:31:59

Making this distinction (that is not mandatory) conveys a meaning for clients of the class and gives hints on their behavior.

private int hugeCapacity(int minCapacity) {...} 

supposes that the method behavior depends on the current instance. So the client class expects that it may manipulate instance members and optionally static members.

while this

private static int hugeCapacity(int minCapacity) {...} 

supposes that the method behavior is a class method that is not related to a specific instance. So it can manipulate only static members.


It is not a great optimization but it spares an additional parameter that is passed to each method by the JVM.
You don't see it in the source code but for instance methods, this is indeed added in the compiled code as parameter.

If you have a method which doesn't use the instance, it is clearer and more efficient to make it static.

Using a instance method which doesn't actually use this can be confusing, and it is potentially more work for the JVM to pass around a reference you don't use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!