Use getters and setters on static fields?

Posted on 2006-05-15
Last Modified: 2010-03-31
Dear Experts,

Is there an "industry standard" for accessing static fields?  I normally use private fields with getters and setters now.  But in the case of static fields, is it worthwhile to bother?  It seems a lot simpler to just do:

String myString = MyClass.SOME_STATIC_FIELD;

and declare SOME_STATIC_FIELD to be public and final.

Question by:BrianMc1958
    LVL 24

    Expert Comment

    The rules for static fields are exactly the same as for instance fields.
    Access restrictions and checking of set-values are just the same!

    LVL 92

    Accepted Solution

    > and declare SOME_STATIC_FIELD to be public and final.

    In that case its a constant so doesn't need a setter, so making it public is acceptable.
    LVL 30

    Assisted Solution

    Abstraction and encapsulation make more sense for data-members, not for static stuff which needs to be publicly accessible.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now