I know what static is, but just not sure when to use it.
我知道静态是什么,但不知道何时使用它。
static variable: I only used it for constant fields. Sometimes there are tens of constants in a class, so using static constants can save lots of memory. Is there any other typical use cases?
静态变量:我只将它用于常量字段。有时类中有几十个常量,因此使用静态常量可以节省大量内存。还有其他典型的用例吗?
static method: I use it when I make a class about algorithms. For example, a class which provides different sorting algorithms. Is it against OOP design? I think it is better to maintain this way rather than implementing sorting algorithms inside each class that needs to use them. Am I wrong? What are some good use cases?
静态方法:我在编写关于算法的类时使用它。例如,提供不同排序算法的类。它是否反对OOP设计?我认为保持这种方式比在每个需要使用它们的类中实现排序算法更好。我错了吗?什么是好用例?
Also, are there any performance difference between using static and non-static fields/methods?
此外,使用静态和非静态字段/方法之间是否存在性能差异?
5 个解决方案
#1
18
You are describing cases where you've used static, but this doesn't quite explain fundamentally why you would use static vs non-static - they are more than just keywords for constants and utility methods.
您正在描述使用静态的情况,但这并不能完全解释为什么要使用静态和非静态 - 它们不仅仅是常量和实用方法的关键字。
When something is not static (instance), it means that there is an instance of it for each instance of the class. Each one can change independently.
当某些东西不是静态的(实例)时,它意味着每个类的实例都有一个实例。每个人都可以独立改变。
When something is static, it means there is only one copy of it for all instances of the class, so changing it from any location affects all others.
当某些东西是静态的时,它意味着它只有一个副本用于该类的所有实例,因此从任何位置更改它会影响所有其他实例。
Static variables/methods typically use less memory because there is only one copy of them, regardless of how many instances of the class you have. Statics, when used appropriately, are perfectly fine in object oriented design.
静态变量/方法通常使用较少的内存,因为它们只有一个副本,无论您拥有多少个类实例。静电,如果使用得当,在面向对象的设计中是完美的。
If you have a method/variable that you only need one instance of (e.g. a constant or a utility method), then just make it static. Understand though that making a method static means it cannot be overridden. So if you have a method you want to override in a subclass, then don't make it static.
如果你有一个方法/变量只需要一个实例(例如常量或实用方法),那么只需将其设为静态。理解虽然使方法静态意味着它不能被覆盖。因此,如果您想要在子类中覆盖一个方法,那么不要将其设置为静态。
The general rule of thumb is - if you need only one copy of it, make it static. If you need a copy per instance, then make it non static.
一般的经验法则是 - 如果您只需要一份副本,请将其设为静态。如果每个实例需要一个副本,则将其设置为非静态。
#2
3
Is there any other typical use cases?
还有其他典型的用例吗?
Global Variables
全局变量
Is it against OOP design?
它是否反对OOP设计?
Not exaclty, the point is that static methods are stateless since you don't need a particular instance of a class. My favorite approach is for utility methods (like Apache Commons). But you may be aware that some methods may be better placed as class members instead of static.
不是很重要,重点是静态方法是无状态的,因为您不需要类的特定实例。我最喜欢的方法是实用程序方法(如Apache Commons)。但是您可能知道某些方法可能更适合作为类成员而不是静态。
Also static methods can make class testability harder once you can't override these methods or replace by mock implementation.
一旦你无法覆盖这些方法或者通过模拟实现替换,静态方法也会使类可测试性变得更难。
Performance difference ?
性能差异?
There's a performance Android recommendation from Google that says "prefer static over virtual":
谷歌推出的性能Android建议称“偏好静态虚拟化”:
http://developer.android.com/training/articles/perf-tips.html#PreferStatic
http://developer.android.com/training/articles/perf-tips.html#PreferStatic
I'm not sure it's true for JVM since Android uses a different VM, but it makes sense given the reasons the link points out:
我不确定JVM是否属实,因为Android使用不同的VM,但考虑到链接指出的原因,它是有道理的:
If you don't need to access an object's fields, make your method static. Invocations will be about 15%-20% faster. It's also good practice, because you can tell from the method signature that calling the method can't alter the object's state."
如果您不需要访问对象的字段,请使您的方法保持静态。调用速度将提高约15%-20%。这也是很好的做法,因为你可以从方法签名中看出,调用方法不能改变对象的状态。“
#3
0
My personal rule of thumb is that static things are "just hanging out there". They are things that (disclaimer, not entirely true) are global, but make sense to include with this one particular class.
我个人的经验法则是静态的东西“只是挂在那里”。它们(免责声明,并非完全真实)是全球性的,但包含在这个特定的类中是有意义的。
Static fields are good if you find yourself loading some heavyweight objects repeatedly. For instance, the project I'm working on now has a toggle between two images. These are static fields that are loaded with the application and kept in memory, rather than reloading them every time and letting GC take care of the mess.
如果您发现自己反复加载一些重量级对象,静态字段就很好。例如,我正在进行的项目现在可以在两个图像之间切换。这些是静态字段,它们与应用程序一起加载并保存在内存中,而不是每次都重新加载它们并让GC处理混乱。
#4
0
Apart from very specific situations, I use static (and final) variables for constants only. It's a totally valid to use them, of course.
除了非常具体的情况,我只使用静态(和最终)变量作为常量。当然,使用它们是完全有效的。
I tend to avoid static utility methods, because they make it harder to write unit tests for the code (mocking the results of the method invocation). When you start developing Test Driven way, this issue becomes quite apparent. I prefer using dependency injection and singleton beans (though it depends on your needs and situation).
我倾向于避免使用静态实用程序方法,因为它们使得为代码编写单元测试变得更加困难(模拟方法调用的结果)。当您开始开发测试驱动方式时,此问题变得非常明显。我更喜欢使用依赖注入和单例bean(虽然这取决于你的需求和情况)。
#5
0
Static variables belong to a class, hence shared by all the objects, so memory usage is less if you really want the varible to be shared. If you declare the variable as public and static, then it is globally available for everyone.
静态变量属于一个类,因此由所有对象共享,因此如果您真的希望共享变量,则内存使用量会减少。如果您将变量声明为public和static,那么它将全局可供所有人使用。
Static methods are generally the utility methods, depending on the access modifier, those can be used within a class or across the classes. Static utility class will help to reduce the memory usage again because you need not to create the object to call those methods.
静态方法通常是实用方法,取决于访问修饰符,可以在类中或跨类使用。静态实用程序类将有助于再次减少内存使用,因为您不需要创建对象来调用这些方法。
#1
18
You are describing cases where you've used static, but this doesn't quite explain fundamentally why you would use static vs non-static - they are more than just keywords for constants and utility methods.
您正在描述使用静态的情况,但这并不能完全解释为什么要使用静态和非静态 - 它们不仅仅是常量和实用方法的关键字。
When something is not static (instance), it means that there is an instance of it for each instance of the class. Each one can change independently.
当某些东西不是静态的(实例)时,它意味着每个类的实例都有一个实例。每个人都可以独立改变。
When something is static, it means there is only one copy of it for all instances of the class, so changing it from any location affects all others.
当某些东西是静态的时,它意味着它只有一个副本用于该类的所有实例,因此从任何位置更改它会影响所有其他实例。
Static variables/methods typically use less memory because there is only one copy of them, regardless of how many instances of the class you have. Statics, when used appropriately, are perfectly fine in object oriented design.
静态变量/方法通常使用较少的内存,因为它们只有一个副本,无论您拥有多少个类实例。静电,如果使用得当,在面向对象的设计中是完美的。
If you have a method/variable that you only need one instance of (e.g. a constant or a utility method), then just make it static. Understand though that making a method static means it cannot be overridden. So if you have a method you want to override in a subclass, then don't make it static.
如果你有一个方法/变量只需要一个实例(例如常量或实用方法),那么只需将其设为静态。理解虽然使方法静态意味着它不能被覆盖。因此,如果您想要在子类中覆盖一个方法,那么不要将其设置为静态。
The general rule of thumb is - if you need only one copy of it, make it static. If you need a copy per instance, then make it non static.
一般的经验法则是 - 如果您只需要一份副本,请将其设为静态。如果每个实例需要一个副本,则将其设置为非静态。
#2
3
Is there any other typical use cases?
还有其他典型的用例吗?
Global Variables
全局变量
Is it against OOP design?
它是否反对OOP设计?
Not exaclty, the point is that static methods are stateless since you don't need a particular instance of a class. My favorite approach is for utility methods (like Apache Commons). But you may be aware that some methods may be better placed as class members instead of static.
不是很重要,重点是静态方法是无状态的,因为您不需要类的特定实例。我最喜欢的方法是实用程序方法(如Apache Commons)。但是您可能知道某些方法可能更适合作为类成员而不是静态。
Also static methods can make class testability harder once you can't override these methods or replace by mock implementation.
一旦你无法覆盖这些方法或者通过模拟实现替换,静态方法也会使类可测试性变得更难。
Performance difference ?
性能差异?
There's a performance Android recommendation from Google that says "prefer static over virtual":
谷歌推出的性能Android建议称“偏好静态虚拟化”:
http://developer.android.com/training/articles/perf-tips.html#PreferStatic
http://developer.android.com/training/articles/perf-tips.html#PreferStatic
I'm not sure it's true for JVM since Android uses a different VM, but it makes sense given the reasons the link points out:
我不确定JVM是否属实,因为Android使用不同的VM,但考虑到链接指出的原因,它是有道理的:
If you don't need to access an object's fields, make your method static. Invocations will be about 15%-20% faster. It's also good practice, because you can tell from the method signature that calling the method can't alter the object's state."
如果您不需要访问对象的字段,请使您的方法保持静态。调用速度将提高约15%-20%。这也是很好的做法,因为你可以从方法签名中看出,调用方法不能改变对象的状态。“
#3
0
My personal rule of thumb is that static things are "just hanging out there". They are things that (disclaimer, not entirely true) are global, but make sense to include with this one particular class.
我个人的经验法则是静态的东西“只是挂在那里”。它们(免责声明,并非完全真实)是全球性的,但包含在这个特定的类中是有意义的。
Static fields are good if you find yourself loading some heavyweight objects repeatedly. For instance, the project I'm working on now has a toggle between two images. These are static fields that are loaded with the application and kept in memory, rather than reloading them every time and letting GC take care of the mess.
如果您发现自己反复加载一些重量级对象,静态字段就很好。例如,我正在进行的项目现在可以在两个图像之间切换。这些是静态字段,它们与应用程序一起加载并保存在内存中,而不是每次都重新加载它们并让GC处理混乱。
#4
0
Apart from very specific situations, I use static (and final) variables for constants only. It's a totally valid to use them, of course.
除了非常具体的情况,我只使用静态(和最终)变量作为常量。当然,使用它们是完全有效的。
I tend to avoid static utility methods, because they make it harder to write unit tests for the code (mocking the results of the method invocation). When you start developing Test Driven way, this issue becomes quite apparent. I prefer using dependency injection and singleton beans (though it depends on your needs and situation).
我倾向于避免使用静态实用程序方法,因为它们使得为代码编写单元测试变得更加困难(模拟方法调用的结果)。当您开始开发测试驱动方式时,此问题变得非常明显。我更喜欢使用依赖注入和单例bean(虽然这取决于你的需求和情况)。
#5
0
Static variables belong to a class, hence shared by all the objects, so memory usage is less if you really want the varible to be shared. If you declare the variable as public and static, then it is globally available for everyone.
静态变量属于一个类,因此由所有对象共享,因此如果您真的希望共享变量,则内存使用量会减少。如果您将变量声明为public和static,那么它将全局可供所有人使用。
Static methods are generally the utility methods, depending on the access modifier, those can be used within a class or across the classes. Static utility class will help to reduce the memory usage again because you need not to create the object to call those methods.
静态方法通常是实用方法,取决于访问修饰符,可以在类中或跨类使用。静态实用程序类将有助于再次减少内存使用,因为您不需要创建对象来调用这些方法。