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:26

To answer your last question, look at this example,

Java Code:

public void test2() {
    setNumber(getNumber() + 1);
}

Byte Code:

public test2()V
   L0
    LINENUMBER 47 L0
    ALOAD 0
    ALOAD 0
    INVOKEVIRTUAL com/test/ByteCodeTester.getNumber()I
    ICONST_1
    IADD
    INVOKEVIRTUAL com/test/ByteCodeTester.setNumber(I)V
   L1
    LINENUMBER 48 L1
    RETURN
   L2
    LOCALVARIABLE this Lcom/test/ByteCodeTester; L0 L2 0
    MAXSTACK = 3
    MAXLOCALS = 1

As you can see, the bytecode still makes the 2 method calls. So the compiler doesn't treat it any differently in this case.

查看更多
等我变得足够好
3楼-- · 2019-07-15 07:31

Getters/setters are nice in that they often may do slightly more than act as the interface to a private field. The value returned/set may be a part of a more complex calculation that goes on behind the scenes.

The danger however, is that because they are fully fledged method calls, they can literally do anything - start a process, download a file, do an expensive task etc. This is of course silly from the context of a getter/setter but is something to avoid doing. I've also heard of code that performs most of what it's meant to do in getters/setters, having no real methods!!

查看更多
Explosion°爆炸
4楼-- · 2019-07-15 07:36

There will only be a difference if the getter/setter does something extra. If you know from the start this will be the case, then use the methods even within the class. If not I'd just go with the direct field manipulation and rely on Eclipse to help me with the refactoring later if necessary.

查看更多
时光不老,我们不散
5楼-- · 2019-07-15 07:37

If the public accessor exists for another purpose, it's preferable to use this accessor, since you'll ensure consistent access to your variable.

However, if your variable does not have a public accessor, it is considered acceptable to access the variable directly. I tend to restrain that to private variables (i.e. no private accessors in the code, but protected accessors instead of protected members if you need access in an implementation of the base class).

So to summarize : I'd always keep the members private and access them through accessors, except if the only accessors needed are private.

查看更多
贪生不怕死
6楼-- · 2019-07-15 07:39
this.number

In most cases they will be optimized to the same code but the point of using setters/getters is to avoid changin API in case of change of implementation. However in class you don't use "API" but you see all the internals and state.

Additionally you can use

numer += 5;

instead of

setNumber(getNumber() + 5);

Edit: Of course outside class it shoudl be protected by getter/setter as you may change internal representation and you can provide backward-compatibility by reimplementing them in terms of new state representation.

Edit 2: main is a bit special. I'd say that main should be as minimal as possible - create some objects and call one or two methods maximum - hence it should not assign variebles and should be threated as 'external' part. On the other hand some provides tests methods in main which may require accessing state directly. So if you can you should not directly access fields in main.

As of speed in main it does not matter anyway as starting the JVM would offset any costs. The real difference would be in the inner loops in which JIT would take care about it.

查看更多
等我变得足够好
7楼-- · 2019-07-15 07:41

number if it is used in the same class. And e.getnumber in any subclasses or anywhere outside

查看更多
登录 后发表回答