Java 使用私有静态方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/685522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:54:47  来源:igfitidea点击:

Using private static methods

javaoopcoding-stylestatic-methods

提问by Andrey Vityuk

What do you think about using private static methods?

您如何看待使用私有静态方法

Personally, I prefer using a staticprivate method to non-staticas long as it does not require access to any instance fields.

就个人而言,我更喜欢使用静态私有方法而不是非静态方法,只要它不需要访问任何实例字段。

But I heard that this practice violates OOP principles.

但是我听说这种做法违反了 OOP 原则。

Edit: I am wondering from style prospective of view, not performance.

编辑:我想从风格的角度来看,而不是性能。

采纳答案by eljenso

A private staticmethod by itself does not violate OOP per se, but when you have a lot of these methods on a class that don't need (and cannot*) access instance fields, you are not programming in an OO way, because "object" implies state + operations on that state defined together. Why are you putting these methods on that class, if they don't need any state?

一个private static单独的方法不违背OOP本身,而是当你有很多的一类,这些方法不需要(也不能*)访问实例字段,你是不是在一个面向对象的方式编程,因为“对象”意味着状态 + 对该状态的操作一起定义。如果这些方法不需要任何状态,为什么要将这些方法放在该类上?

(*) = In principle, due to the class level visibility in Java, a static method on a class hasaccess to instance fields of an object of that class, for example:

(*) = 原则上,由于 Java 中的类级别可见性,类上的静态方法可以访问该类的对象的实例字段,例如:

class Test
{
  int field = 123;

  private static void accessInstance(Test test)
  {
    System.out.println(test.field);
  }
}

You need to pass in the reference to an instance (thispointer) yourself of course, but then you are essentially mimicking instance methods. Just mentioning this for completeness.

当然,您需要自己传递对实例(this指针)的引用,但是您实际上是在模仿实例方法。只是为了完整性而提到这一点。

回答by Eric Petroelje

I don't necessarily see any real problem with what you are doing, but my first question would be if the method doesn't require access to any instance fields, then what is it doing in that class in the first place?

我不一定认为您正在做的事情有任何真正的问题,但我的第一个问题是,如果该方法不需要访问任何实例字段,那么它首先在该类中做什么?

回答by soulmerge

It's a matter of taste, but I make methods that don't react to a state within the object static. This way I don't have to rewrite code if a static function needs similar functionality. A sorting function would be a good example of such a case.

这是一个品味问题,但我制作的方法不会对静态对象内的状态做出反应。这样,如果静态函数需要类似的功能,我就不必重写代码。排序函数将是这种情况的一个很好的例子。

回答by Michael Borgwardt

private or public doesn't make a difference - static methods are OK, but if you find you're using them all the time (and of course instance methods that don't access any instance fields are basically static methods for this purpose), then you probably need to rethink the design. It's not always possible, but most of the time methods should reside with the data they operate on - that's the basic idea of OOP.

private 或 public 没有区别 - 静态方法是可以的,但如果你发现你一直在使用它们(当然,不访问任何实例字段的实例方法基本上是用于此目的的静态方法),那么您可能需要重新考虑设计。这并不总是可能的,但大多数时候方法应该与它们操作的数据一起存在——这是 OOP 的基本思想。

回答by digitalsanctum

I tend not to use private static methods. I do use public static methods and group them into Util classes to promote reuse.

我倾向于不使用私有静态方法。我确实使用公共静态方法并将它们分组到 Util 类中以促进重用。

回答by eljenso

Private static methods can for example operate on private static members of their class. This can be utilized to encapsulate and unify certain class specific operations.

例如,私有静态方法可以对其类的私有静态成员进行操作。这可用于封装和统一某些特定于类的操作。

The major drawback of using static methods is in my opinion the fact that one throws away the possibility to override. Since classes in Java are not like, let's say, classes in Smalltalk, you can not override static methods.

在我看来,使用静态方法的主要缺点是放弃了覆盖的可能性。由于 Java 中的类不像 Smalltalk 中的类,因此您不能覆盖静态方法。

Since your question relates to private static methods, overriding is out of option anyway.

由于您的问题与私有静态方法有关,因此无论如何都无法进行覆盖。

I tend to use static methods only in case of utility classes (like java.lang.Math) or patterns like the Singleton pattern. All of these require a higher visibility than private, because they represent services provided by their class to others.

我倾向于仅在实用程序类(如 java.lang.Math)或单例模式等模式的情况下才使用静态方法。所有这些都需要比私有更高的可见性,因为它们代表自己的类向其他人提供的服务。

Final thought: If you have one or more private static methods, think about extracting them to a dedicated utility class and making them public. Even better, make them instance methods and use the Singleton pattern.

最后的想法:如果您有一个或多个私有静态方法,请考虑将它们提取到专用的实用程序类并将它们公开。更好的是,让它们成为实例方法并使用单例模式。

回答by tyler

As mentioned above, private static methods are often useful for organizing re-used logic and reducing/eliminating repeated code. I'm surprised that I haven't noticed any mention of performance in this discussion. From Renaud Waldura's 'The Final Word on Final':

如上所述,私有静态方法对于组织重用逻辑和减少/消除重复代码通常很有用。我很惊讶我没有注意到在这个讨论中提到任何性能。来自 Renaud Waldura 的“The Final Word on Final”:

(Note, private static methods are implicitly final)

(注意,私有静态方法是隐式最终的)

"Since a final method is only implemented in the declaring class, there is no need to dynamically dispatch a call to a final method, and static invocation can be used instead. The compiler can emit a direct call to the method, bypassing entirely the usual virtual method invocation procedure. Because of this, final methods are also candidates for inlining by a Just-In-Time compiler or a similar optimization tool. (Remember, private/static methods are already final, therefore always considered for this optimization.)"

“由于final方法只在声明类中实现,因此不需要动态调度对final方法的调用,而可以使用静态调用。编译器可以直接调用该方法,完全绕过通常的虚拟方法调用过程。因此,最终方法也是即时编译器或类似优化工具内联的候选方法。(请记住,私有/静态方法已经是最终方法,因此始终考虑进行此优化。)“

Check out the whole paper: http://renaud.waldura.com/doc/java/final-keyword.shtml

查看整篇论文:http: //renaud.waldura.com/doc/java/final-keyword.shtml