Static fields accessed by getter/setter...what happens?  
Author Message
stacnebben





PostPosted: 2004-2-10 22:06:00 Top

java-programmer, Static fields accessed by getter/setter...what happens? (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
 
Collin VanDyck





PostPosted: 2004-2-10 22:29:00 Top

java-programmer >> Static fields accessed by getter/setter...what happens? 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

"Stacey" <email***@***.com> wrote in message
news:email***@***.com...
> (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