Static fields accessed by getter/setter...what happens?

S

Stacey

(apology for duplicate post, I felt I posted in the wrong forum earlier.)
Could someone please enlighten me as to what will happen if I have:

public class MyClass
{
public static String someName = null;

public void setSomeName(String s)
{
someName = s;
}

public void getSomeString()
{
return someString;
}
}

I'm not sure how the static field is being handle using non-static getters/setters.
Thanks
Stacey
 
C

Collin VanDyck

Well, the first response should be, "what happened when you tried it?".

Past that, your class instances do have access to static variables. As long
as you did not declare someName to be final, your instances can easily
change it. Be careful, though, about your setters -- if you are allowing
hundreds of instances to modify the same variable, you should probably
synchronize your setters.

That having been said, I would probably make my getters and setters also
static in this case.

Collin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top