1

In regards to Java, I'm very accustomed to declaring all of my variables as private and generating public getters and setters to hold true to common convention.

I find it curious, though: in relation to getters and setters with no functionality outside of assigning and returning the requested value, is there no performance hit for calling methods like:

String getValue() { return value; } 

instead of:

classInstance.value; 

Does the compiler do something here to help keep the function call from adding an extra cycle? If not, then doesn't this theoretical hit snowball into a large hit in more robust applications?

Edit:: For clarification, this question is not asking why you should or shouldn't use accessor methods, the question is whether or not you take a performance hit by using accessors.

7
  • 1
    very wide question, but generally don't use micro optymalisation, clean readability is more important. Commented Aug 11, 2017 at 14:37
  • In the scope of any sufficiently complex program, I would think the overhead is negligible Commented Aug 11, 2017 at 14:38
  • I didn't think it was too broad. Does defining a getter and setter method over using public variables add an extra cycle of work in Java? Personally, I don't think that classInstance.getValue() is any less readable than classInstance.value; Commented Aug 11, 2017 at 14:38
  • 1
    @anomeric Well, there are actually many good reasons to consider using accessors. Commented Aug 11, 2017 at 14:40
  • 1
    This really a question from another era. The things that slow programs down now are issues like database caching, multi-threading, and performance scaling Commented Aug 11, 2017 at 14:47

2 Answers 2

4

Does the compiler do something here to help keep the function call from adding an extra cycle?

Yes.

A Hotspot JIT compiler will inline short methods, and a simple getter or setter is short enough to allow that. Inlining the method body gets rid of the overheads of the parameter assembly and the method call, and allows further local optimization in the context of the calling method.

The net result is that using getters and setters is not a performance hit on a modern JVM(tm)

(Some early Android compilers didn't do this, but this has been rectified.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for actually addressing the question.
0

I don't know if Java does something like that, but C++ can inline that sort of stuff.

I also like to use getters and setters, but they are slower than plain variables.

So go with getters and setters if you're creating an API and choose public variables if you need performance and readability is not that important.

3 Comments

impossible to say getter is slower, JMV optimise when code is highly executed
I once did an experiment to find out, which showed that getters and setters were slower. But that may have been improved.
If your experiments showed a measurable difference for any recent version of Java, there is most likely a problem with your benchmarking techniques.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.