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

Discussion in 'Java' started by Stacey, Feb 10, 2004.

  1. Stacey

    Stacey Guest

    (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
     
    Stacey, Feb 10, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > (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, Feb 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raoul Markus
    Replies:
    3
    Views:
    1,113
    Roedy Green
    Sep 20, 2003
  2. Timo Nentwig
    Replies:
    75
    Views:
    3,360
    Jon A. Cruz
    Jan 18, 2004
  3. Timo Nentwig

    EL: accessing non-getter/setter

    Timo Nentwig, May 10, 2004, in forum: Java
    Replies:
    6
    Views:
    666
    Murray
    May 11, 2004
  4. Adam Sandler

    problem with getter and setter not working

    Adam Sandler, May 25, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    790
    Jimi200478
    May 26, 2006
  5. gamehack
    Replies:
    2
    Views:
    507
    James Stroud
    Jul 18, 2007
Loading...

Share This Page