开发者

How to modify private static variable through setter method

I have the following variable in a class named 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 me开发者_如何转开发thod in Example static so I can access it like this:

Example.setNumber(3);

2) or Make the setter method non-static so I create an object of Example to set the number

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. 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. 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. Use the first option or just make the number public, depending on your needs.


The setter of a static variable that do not depends on any instance variables/functions should be also static. So 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. (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. But regular setter in this case may confuse user: the naming convention says that setter changes value of field that belongs to specific instance. Therefore you should use the first version: 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.

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. Tell us where the setNumber method will be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜