将方法声明为静态有什么好处 [英] What is the gain from declaring a method as static

查看:25
本文介绍了将方法声明为静态有什么好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在 Eclipse 中查看了我的警告并遇到了这个警告:

I've recently been looking through my warnings in Eclipse and come across this one:

如果该方法可以声明为静态,它将给出编译器警告.

It will give a compiler warning if the method can be declared as static.

Eclipse 帮助中的准确引用,强调私有和最终:

[edit] Exact quote within the Eclipse help, with stress on private and final:

启用后,编译器将发出错误或警告privatefinal 且仅引用静态的方法会员.

When enabled, the compiler will issue an error or a warning for methods which are private or final and which refer only to static members.

是的,我知道我可以关闭它,但我想知道打开它的原因?

Yes I know I can turn it off, but I want to know the reason for turning it on?

为什么将所有可能的方法声明为静态方法是一件好事?

这会带来任何性能优势吗?(在移动域中)

Will this give any performance benefits? (in a mobile domain)

指出一个方法是静态的,我想这表明您不使用任何实例变量,因此可以移至 utils 样式类?

Pointing out a method as static, I suppose is showing that you don't use any instance variables therefore could be moved to a utils style class?

在一天结束时,我应该关闭它忽略"还是应该修复它给我的 100 多个警告?

At the end of the day should I just turn this off 'ignore' or should I fix the 100+ warnings it has given me?

您是否认为这只是使代码的额外关键字,因为编译器无论如何都会内联这些方法?(有点像你没有声明每个变量你可以最终但你可以).

Do you think this is just extra keywords that dirty the code, as the compiler will just inlines these methods anyway? (kind of like you don't declare every variable you can final but you could).

推荐答案

每当你编写一个方法时,你就在给定的范围内履行了一个契约.范围越窄,你写错误的机会就越小.

Whenever you write a method, you fulfill a contract in a given scope. The narrower the scope is, the smaller the chance is that you write a bug.

当一个方法是静态的,你不能访问非静态成员;因此,您的范围更窄.因此,如果您不需要并且永远不需要(甚至在子类中)非静态成员来履行您的合同,为什么要让您的方法访问这些字段?在这种情况下声明方法 static 将使编译器检查您没有使用您不打算使用的成员.

When a method is static, you can't access non-static members; hence, your scope is narrower. So, if you don't need and will never need (even in subclasses) non-static members to fulfill your contract, why give access to these fields to your method? Declaring the method static in this case will let the compiler check that you don't use members that you do not intend to use.

此外,它将帮助阅读您的代码的人了解合同的性质.

And moreover, it will help people reading your code understand the nature of the contract.

这就是为什么在实际实现静态合约时声明方法 static 被认为是好的原因.

That's why it's considered good to declare a method static when it's actually implementing a static contract.

在某些情况下,您的方法仅表示与您的类的实例相关的某些内容,而它的实现实际上并不使用任何非静态字段或实例.在这种情况下,您不会将方法标记为 static.

In some cases, your method only means something relative to an instance of your class, and it happens that its implementation doesn't actually use any non-static field or instance. In such cases, you would not mark the method static.

不使用 static 关键字的示例:

Examples of where you would not use the static keyword:

  • 一个什么都不做的扩展钩子(但可以对子类中的实例数据做一些事情)
  • 一个非常简单的默认行为,可以在子类中进行自定义.
  • 事件处理程序实现:实现将因事件处理程序的类而异,但不会使用事件处理程序实例的任何属性.

这篇关于将方法声明为静态有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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