[英]How to modify private static variable through setter method
I have the following variable in a class named Example: 我在名为Example的类中有以下变量:
private static int number;
If I wanted to assign the variable a number using an outside class, which would I do? 如果我想使用外部类为变量赋值,我会这样做吗?
1) Make the setter method in Example static so I can access it like this: 1)使示例静态的setter方法,所以我可以像这样访问它:
Example.setNumber(3);
2) or Make the setter method non-static so I create an object of Example to set the number 2)或使setter方法非静态,所以我创建一个Example对象来设置数字
Example e = new Example()
e.setNumber(3);
What are the differences between the two and which one is the better way? 两者之间有什么区别,哪一种更好?
It is recommendable to use a static method in this case. 在这种情况下,建议使用静态方法。
Why? 为什么? Well, if you make it a non-static method, that would lead to the following suprising effect:
好吧,如果你使它成为一个非静态的方法,那将导致以下惊人的效果:
Example e1 = new Example();
Example e2 = new Example();
e2.setNumber(3);
e1.setNumber(5);
System.out.println(e2.getNumber()); // surprise! prints 5,
So even though you called the method on e1, e2 is also affected. 所以即使你在e1上调用了这个方法,e2也会受到影响。 The corresponding static example is much less surprising:
相应的静态示例不那么令人惊讶:
Example e1 = new Example();
Example e2 = new Example();
Example.setNumber(5);
System.out.println(Example.getNumber()); // prints 5, no surprise...
First of all, you really shouldn't be setting static variables. 首先,你真的不应该设置静态变量。 It's prone to cause problems, and it's generally indicative of bad design.
它容易引起问题,并且通常表明设计不良。 The only times static variables should be used are for thread-safe immutable objects and singletons.
应该使用静态变量的唯一时间是线程安全的不可变对象和单例。
That said, if you absolutely still want to set the value, make it a static method, ince you shouldn't need to instantiate the object in order to set a static value. 也就是说,如果您仍然想要设置值,请将其设置为静态方法,因此您不需要实例化对象以设置静态值。
The first one would be the correct one. 第一个是正确的。 When you access a static method, you use the class name and not an object refrence
当您访问静态方法时,您使用类名而不是对象引用
If it's a static variable, make the setter static. 如果它是静态变量,请将setter设为静态。 Having to create an instance just to modify something that belongs to the whole class is both verbose and wasteful.
必须创建一个实例来修改属于整个类的东西既冗长又浪费。
Please don't use the second option. 请不要使用第二个选项。 Creating an instance just for an assignment is a crime:P.
仅为作业创建实例是犯罪:P。 Use the first option or just make the
number
public, depending on your needs. 根据您的需要,使用第一个选项或公开
number
。
The setter of a static variable that do not depends on any instance variables/functions should be also static. 不依赖于任何实例变量/函数的静态变量的setter也应该是静态的。 So 1).
所以1)。
But beware of creating global variables! 但要注意创建全局变量!
There is no point to creating an instance of a class just to set a static variable on it. 没有必要创建一个类的实例只是为了设置一个静态变量。 I would go with #1.
我会选择#1。 (Although I try to avoid global variables, which is what the static variable is.)
(虽然我试图避免全局变量,这是静态变量。)
Static member is the same for all instances of class. 所有类的实例的静态成员都是相同的。 You can change is either using static or regular setter.
您可以使用静态或常规setter进行更改。 But regular setter in this case may confuse user: the naming convention says that setter changes value of field that belongs to specific instance.
但是在这种情况下,常规setter可能会使用户感到困惑:命名约定表明setter会更改属于特定实例的字段的值。 Therefore you should use the first version:
Example.setNumber(3)
. 因此,您应该使用第一个版本:
Example.setNumber(3)
。
Static variables are made static because they are not associated with any particular object. 静态变量是静态的, 因为它们不与任何特定对象相关联。
Both approaches work, but the former is more sensible, because it does not require an arbitrary object to be created and used. 这两种方法都有效,但前者更为明智,因为它不需要创建和使用任意对象。
The consensus of other posters is for #1 static method. 其他海报的共识是#1静态方法。
I will argue that we can not answer the question with available information. 我认为我们无法用现有信息回答这个问题。 If for example, the setNumber method is necessary to implement an interface then it should be #2 instance method.
例如,如果需要setNumber方法来实现接口,那么它应该是#2实例方法。 Tell us where the setNumber method will be used.
告诉我们将使用setNumber方法的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.