Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Static fields accessed by getter/setter...what happens?

Reply
Thread Tools

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

 
 
Stacey
Guest
Posts: n/a
 
      02-10-2004
(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
 
Reply With Quote
 
 
 
 
Collin VanDyck
Guest
Posts: n/a
 
      02-10-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> (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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static field should be accessed in a static way ireallyneedtoknow2007@yahoo.com Java 3 04-16-2008 09:34 AM
why static data cannot be accessed from other programs file hayyal@alcatel-lucent.com C++ 3 10-11-2007 08:21 PM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
referring to static fields of enclosing class from static inner class? bugbear Java 4 08-23-2006 08:26 AM
Why does DJ v3.7.7.81 generate weird-looking static fields like class$exercise$MyClass and static methods _mthclass$(String)? Bruce .J Sam Java 0 06-16-2005 02:34 AM



Advertisments