为什么存在私有静态方法,那些在任何静态上下文中都没有被调用? [英] why private static methods exists those are not being called in any static context?

查看:303
本文介绍了为什么存在私有静态方法,那些在任何静态上下文中都没有被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于静态事物在类加载时加载,甚至可以在创建对象之前使用 ClassName.member

Since static things loaded at time of class loading,and can be used even before object creation as ClassName.member

但是如果静态方法是私有的,那么你只能在类中执行此操作 ClassName.method(),这也可以直接通过方法访问(),不附加类名。

but if static method is private,so you can only do this thing ClassName.method() inside class which is also accessible by directly method() without appending classname.

因此制作私有方法 static 没有任何意义,直到它没有在同一个类的任何 static 方法中使用。因为只有private才能在其他一些方法中使用同一类的静态方法。

Hence making a private method static has no significance untill it is not being used in any static method of same class.since only private can not be use in some other static method of same class.

但是我看到一些方法没有在任何其他静态的东西中使用,但仍然是 static 。例如 hugeCapacity 方法> ArrayList -

But I seen some method those are not being use in any other static stuff and still they are static.For Example hugeCapacity method of ArrayList-

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

为什么我们不将它仅保密?

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

可以让我知道背后的意义,制作这个方法在我们的java库中是静态的吗?

Can one let me know significance behind, making this method static in our java Libraries?

推荐答案

进行这种区分(这不是强制性的)为类的客户传达了一个含义并提供有关其行为的提示。

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.

这个

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.

这不是一个很好的优化,但它可以节省JVM传递给每个方法的附加参数。


你没有在源代码中看到它,但是对于实例方法,这个确实在编译代码中作为参数添加。

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.

这篇关于为什么存在私有静态方法,那些在任何静态上下文中都没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆