何时使用静态变量/方法以及何时在Java中使用实例变量/方法? [英] When to use static variables/methods and when to use instance variables/methods in Java?
问题描述
我想问一下这个问题,什么时候使用静态变量/方法或者在Java中的其他情况下实例变量/方法是有利的?
i would like to ask the question when it would be advantageous to use static variables/methods or in the other case instance variables/methods in Java?
我知道这取决于某些情况(比如将util-classes编程为静态方法),但是我们可以声明类似于一般策略的东西吗?
I know that it depends on the certain case (like programming util-classes as static methods), but can we declare something like a general strategy?
推荐答案
在新手级别:
在以下情况下使用实例变量:每个变量对于不同的对象具有不同的值。例如:学生姓名,卷号等。
Use instance variables when : Every variable has a different value for different object. Eg : name of student, roll number etc..
在以下情况下使用静态变量:变量的值独立于对象(对于每个对象不唯一)。例如:没有学生。
use static variables when : The value of the variable is independent of the objects (not unique for each object). Eg: no of students.
这篇关于何时使用静态变量/方法以及何时在Java中使用实例变量/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!