开发者

What is a good practice to access class attributes in class methods?

I always wonder about the best way to access a class attribute from a class method in Java.

Could you quickly convince me about whic开发者_运维问答h one of the 3 solutions below (or a totally different one :P) is a good practice?

public class Test {

    String a;


    public String getA(){
        return this.a;
    }

    public setA(String a){
        this.a = a;
    }

    // Using Getter
    public void display(){

        // Solution 1
        System.out.println(this.a);

        // Solution 2
        System.out.println(getA());

        // Solution 3
        System.out.println(this.getA());
    }


    // Using Setter
    public void myMethod(String b, String c){

        // Solution 1
        this.a = b + c;

        // Solution 2
        setA(b + c);

        // Solution 3
        this.setA(b + c);
    }
}


That entirely depends on what the getters and setters are doing. If they do more than just getting and setting the value (which should be explicitly documented in the method's Javadoc), then it would really make difference what way you'd choose from inside the class. But if they are pure Javabean like getters/setters, then I'd rather access the variable directly by either a or this.a depending on whether there's a local variable in the scope with exactly that name.

Personally I would just keep the getters and setters "pure" according the Javabean spec and add another getter or setter with a self-explaining method name whenever I'd like to do something more than just getting/setting the value. E.g. getAndIncrement(), getAsString(), setAsInt(String), etc.

Matter of taste. It won't really harm as long as you're consistent with it throughout your coding.


If you need to create any validation in the future you'll want a setter/getter . When you make a variable visible you brake the class encapsulation. In theory it means that your code is not so Object Oriented :P , but in practice you lose the ability to do a lot of code refactorings. For example, extracting a interface. And for the this call, I think its redundant, but that's just me :)


I use getters and setters in a class if there is more logic involved that just returning this.value. This way I avoid duplication. An example:

...
public List<String> getList() {

    if (this.list == null) {
        this.list = new LinkedList<String>();
    }

return this.list;

}

public int getListSize() {
    return getList().size();
}
...

I always use "this" because it makes it easier for other people to read my code. There is no doubt that this.value is a class attribute, whereas value can be both a local variable and a class attribute.


Solution 2 or 3 are best practice as they provide encapsulation to the field. For example, what if the field 'a' is a user's postcode and your application has a new requirement to always return the postcode as uppercase. With solutions 2 or 3 this becomes trivial. E.g.

private String postcode;
public String getPostcode()
{
   return postcode;
}

becomes

private String postcode;
public String getPostcode()
{
   return postcode != null? postcode.toUppercase() : null;
}

and you will only have made the change in one place instead of anywhere where the field is accessed. The addition of this is purely up to your own personal style or project standards. Personally, I don't like it as it is unnecessary and just gets in the way of readability, but for others it makes the owner of method/field clearer.


Using setA(b + c) is silly.

Getters and setters are part of the interface. Methods already have full access to the state. Be frank about it.

If you're worried that you might break an invariant then your class is too complex for you. (Admit it and refactor.)


Using getters and setters is the way to go.

  • It's commonly accepted practise

So other programmers are more likely to understand your code.

  • It gives the class author options in the future

Say you want to prevent someone setting a to null. Expose the member and you can never do it.

As for whether to use this - I try to use this consistently to make it very clear to anyone else which are instance members and which are local variables at any point - also helps avoid accidental shadowing, but I think this is less important and more a style thing.

Also - this.a is an instance member (one per instance) not a class member (one-per-class, would be static). Another reason to use this to be clear.


I would go with

System.out.println(getA());

and

setA(b + c);

for the simple reason that if you wanted to generally change the way an attribute is accessed or enforce any constraints as to what you could set a variable to, you can just change the getA or setA methods.

I don't like using this unless I need to explicitly distinguish between variables of the same name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜