Java Conventions: use getters/setters WITHIN the class?

前端 未结 12 1777
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 02:32

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the ge

相关标签:
12条回答
  • 2020-11-30 02:37

    If your class (or the accessor methods in question) is not final, then you should definitely use the accessor methods.

    If another class extends yours and overrides those accessors, your class should use the overridden accessors. If this would break your superclass, then your superclass is designed incorrectly; prevent those accessors from being overridden with final, or change the design of your class.

    0 讨论(0)
  • 2020-11-30 02:46

    No, you don't. You can access any variables, private, public or protected, from within the class.

    Here are some tables to help you:

    enter image description here

    Source: Java Tutorials

    0 讨论(0)
  • 2020-11-30 02:47

    It's normally not a good idea, for a number of reasons:

    • You may not even want accessors for all fields
    • Some accessors may make a defensive copy so not to expose internal state, this is normally unnecessary within the class where you know that you are not going to modify it - or plain wrong if you know you ARE going to modify it
    • It makes debugging more annoying, because you have to follow the getters / setters
    • It makes reading the code harder in most IDEs, since most of them color fields differently than local variables

    ... but as always, there are exceptions. Some setters may have side-effects (for example setting a second value) that you WANT to execute, then it might be better to use the setter. Also, if you design your class for inheritance, it may be better to go via an accessor if you want the subclass to be able to alter the behavior.

    0 讨论(0)
  • 2020-11-30 02:55

    You CAN do either one. However, your professor might appreciate using the methods instead of the direct access. Here's why.

    Let's say you have a class like this:

    class SomeClass {
        private int someValue;
        private String someString;
    
        public SomeClass(int someValue, String someString) {
            this.someValue = someValue;
            this.someString = someString;
        }
    
        public int someValue() {
            return this.someValue;
        }
    
        public String someString() {
            return this.someString;
        }
    
        public String toString() {
            return someValue + ": " + someString;
        }
    
    }
    

    It's pretty straightforward, right? Well, what if all of a sudden we want to CHANGE the implementation of how we calculate someValue, and base it off of someString:

    public int someValue() {
        int value = 0;
        for(int i = 0; i < someString.length; i++) {
             if(someString.charAt(i) == ' ') value++;
        }
        return value;
    }
    

    Now you also have to change every place where variable someValue was used.

    So if you want to make the code easier to maintain in the long run, use the methods calls. This way when you code changes on you (and trust me, it changes all the time) you only have to change it in one spot instead of two.

    And yes, you would want to use a method call in getting someString instead of the direct access in the last method :-)

    0 讨论(0)
  • 2020-11-30 02:55

    I use a mix of both. Accessor methods add more clutter so I use them only when the variable is used many times. If the variable is used only once or twice I don't use them.

    0 讨论(0)
  • 2020-11-30 02:56

    No you can use directly your instance variables inside the class, you're not violating any "rule". Getters and setters are mandatory for others classes to access instance variables of a class to not violate the encapsulation principle (which is quite important in OO programming).

    In the end it's a matter of choice, but you're saving one method call using your first example.

    0 讨论(0)
提交回复
热议问题