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;
To answer your last question, look at this example,
Java Code:
Byte Code:
As you can see, the bytecode still makes the 2 method calls. So the compiler doesn't treat it any differently in this case.
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!!
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.
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.
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
instead of
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 thatmain
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 inmain
which may require accessing state directly. So if you can you should not directly access fields inmain
.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.number if it is used in the same class. And e.getnumber in any subclasses or anywhere outside