I am wondering if using the accessor methods of a class within the class itself will cause any performance problems. I am wondering about Java compilers specifically, but I suppose this is somewhat language agnostic.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
AFAIK, newer JVM’s are very good at optimizing the bytecode on start up and in-the-fly.
Documentation from J2SE SDK v 1.4.2 (which is already very old, like close to a decade) already mentions that the JVM will inline accessor-calls within a class:
“The Java 2 release of the Java VM automatically inlines simple methods at runtime. In an un-optimized Java VM, every time a new method is called, a new stack frame is created. The creation of a new stack frame requires additional resources as well as some re-mapping of the stack, the end result is that creating new stack frames incurs a small overhead.
Method inlining increases performance by reducing the number of method calls your program makes. The Java VM inlining code inlines methods that return constants or only access internal fields. “ (emphasis mine) J2SE SDK 1.4.2_02 Chapter 8 Continued: Performance Features and Tools
Also, I’d make sure that the worst bottleneck of the software actually is < insert whatever you feel is “slow”, in this case using accessors within a class >. IMHO, premature optimization is bad, optimizing by guess is even worse, so try to profile and measure that the bottleneck actually is where you think it is, before trying to fix it.