Sujen
Sujen

Reputation: 1664

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 method 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?

Upvotes: 14

Views: 37050

Answers (10)

amarillion
amarillion

Reputation: 24937

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...

Upvotes: 13

Reverend Gonzo
Reverend Gonzo

Reputation: 40851

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.

Upvotes: 5

emory
emory

Reputation: 10891

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.

Upvotes: 1

user849058
user849058

Reputation:

The first one would be the correct one. When you access a static method, you use the class name and not an object refrence

Upvotes: 4

AlexR
AlexR

Reputation: 115378

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).

Upvotes: 2

Ray Toal
Ray Toal

Reputation: 88428

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.

Upvotes: 1

Petar Minchev
Petar Minchev

Reputation: 47383

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.

Upvotes: 3

Nathan Hughes
Nathan Hughes

Reputation: 96424

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.)

Upvotes: 2

Etienne de Martel
Etienne de Martel

Reputation: 36984

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.

Upvotes: 3

marc
marc

Reputation: 6223

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!

Upvotes: 2

Related Questions