java java中的实用函数是什么意思?它与静态有什么关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25060976/
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-11-02 07:15:21  来源:igfitidea点击:

what do you mean by utility functions in java?how it is related to Static?

java

提问by sanath sanu

hi what is the actual meaning of utility function in java?

嗨,java中效用函数的实际含义是什么?

回答by Chris K

The term utility function was used by Joshua Blochin the book Effective Javato describe the methods on classes such as Arrays, Objectsand Math.

Joshua BlochEffective Java一书中使用了效用函数这个术语来描述诸如数组对象数学之类的类的方法。

The approach predates Java, and is defined by Wikipedia - Utility Classas a set of methods that perform common, often reused functions. I would go on to also point out that the functions tend to require no Object level state, that is they tend to be global functions. And that is why, in Java they tend to become implemented as static methods on a container class. As that way there is no need to instantiate the class first, and it implies through convention that the method should not have any side effects. Of course counter examples of this probably exist, but not to my knowledge within the Java Core libraries.

该方法早于 Java,并由Wikipedia - Utility Class定义为一组执行常见、经常重用功能的方法。我还要继续指出,函数往往不需要对象级别的状态,也就是说,它们往往是全局函数。这就是为什么在 Java 中,它们倾向于在容器类上实现为静态方法。这样就不需要首先实例化类,并且通过约定暗示该方法不应该有任何副作用。当然,这方面的反例可能存在,但据我所知在 Java 核心库中并不存在。