Java language convention; getters/setters

2019-07-15 06:50发布

Public class Example {

    private int number;

    public Example(int number){
        this.number = number;
    }

    public int getNumber(){
        return number;
    }

    public void setNumber(int number){
        this.number = number;
    }

    public static void main(String[] args){
        Example e = new Example(5);

What is preffered when accessing a variable within its own class; "e.number" or "e.getNumber()" ?

Edit:
I think the most important question is: does the compiler know the method you call is a getter or setter. So, will e.setNumber(5); be as fast as e.number = 5;

12条回答
迷人小祖宗
2楼-- · 2019-07-15 07:42

The JVM will optimize away the method call of the setter whenever it is safe to do so (i.e. under almost any case where you would be tempted to just use the field). So for performance, it ought not matter unless you're running on a JVM that doesn't do JIT compilation (e.g. Dalvik). In fact, you can chain method calls many levels deep and the JVM will optimize it away to just a field access when it actually runs it on hardware.

So you generally need not worry about performance costs associated with getters/setters unless they are genuinely doing something extra (which, here, they are not). You can instead make the decision based on other criteria (do you want the flexibility to change the underlying representation? do you want to redefine it in later classes, and if so, is the redefined setter/getter correct, or is the field access correct? etc.).

查看更多
Ridiculous、
3楼-- · 2019-07-15 07:44

I would say it depends on the situation. If the field is something simple such as an int and unlikely to change in the future, I would access it using number and not getNumber().

If the field represents something more involved that could in some situation perhaps be computed in future situation or possibly overridden in a subclass, getNumber() is the obvious choice.

My rule of thumb: If there is any remote chance that I can benefit from going through getNumber() I use getNumber(), otherwise I use number for clarity and brevity.

查看更多
地球回转人心会变
4楼-- · 2019-07-15 07:44

e.getNumber() is the common approach. So get or set + VariableName.

You could also have a look HERE.

查看更多
叼着烟拽天下
5楼-- · 2019-07-15 07:49

say goodbye to multithreading with this.number=.. not to mention eventual need for implementing control of what can be inside this.number. With setNumber() Java concludes this method could be final and can make alot with optimization so you can't sense the difference...

查看更多
孤傲高冷的网名
6楼-- · 2019-07-15 07:50

I generally prefer to still go through the setters/getters, even when accessing the class internally.

I do this because there may be times I want to modify the getters so that it does more than just retrieving a property (e.g., lazy load variable), and I want to have the flexibility of being able to do that without having to worry about modifying other parts of the class.

That said, there are exceptions, though these are for the most part, more personal preference than general best practice. I'll access member variables directly:

  1. When implementing what I consider "low level" functionality (and these are totally subjective, in my case, I consider #equals(), #hashCode(), #toString() and the like "low level"). I don't think there's a very strong technical reason for this, it just "feels" better to me.
  2. If it is way too inconvenient to use the setters/getters (e.g., num ++ vs setNumber(getNumber() + 1) as someone else pointed out).
  3. If my getter does do some additional work, and in my particular case, I do not want that behavior.
  4. ...And there are probably other cases I've missed...
查看更多
放荡不羁爱自由
7楼-- · 2019-07-15 07:51

number

Except number is a private member of a base class. Then i use the getter/setter to make clear, its not a member of the extending class.

Edit: Oh you ment using it in the main function inside the class? Then ofc e.getNumber() as the other guy said.

查看更多
登录 后发表回答