In java programming, method level access modifiers using protected or public where private can be used will affect the performance in any way? if so in what what way it affects the run time performance.
Asked
Active
Viewed 907 times
1
-
1See also: http://stackoverflow.com/questions/4279420/does-use-of-final-keyword-in-java-improve-the-performance – lexicore Oct 31 '14 at 13:52
-
2See also: http://stackoverflow.com/questions/7365329/speed-optimizing-private-and-public-variables-java – lexicore Oct 31 '14 at 14:01
2 Answers
2
Quoting erickson from the following answer:
The access modifier on the field doesn't make any difference in speed, but invoking the accessor method does.
ps. Please upvote the original answer instead of this one.
0
Even if there is a theoretical difference, I doubt that there is a measurable diffrence. At least in any sensible Java runtime

David Soroko
- 8,521
- 2
- 39
- 51
-
Do you happen to have any benchmark to the benchmark to support your statement? – lexicore Oct 31 '14 at 14:07
-
This is really a JVM implementation detail and I imagine that there could be an implementation with non trivial differences. I would consider this a bug. What sort of benchmark are you looking for? – David Soroko Oct 31 '14 at 14:13
-
The one which would support or rebut your statement. :) Otherwise it is baseless. – lexicore Oct 31 '14 at 14:17
-
Why would I want to rebut my own statement? For support, we prove the non-existence of such behavior. For this we will need to agree on JDK and OS. Make sure that the contribution of hotspot is well understood. Make sure that various structural scenarios (e.g. sub classing, final classes/parameters, parameter types, etc...) are covered. All in all too many dimensions to cover. Of course, a single example will suffice to prove the opposite which is really the base for my position, if you have one, I will be very interested to see it. – David Soroko Oct 31 '14 at 14:38
-
"A single example will suffice to prove the opposite" is a classic [argumentum ad ignorantiam](http://en.wikipedia.org/wiki/Argument_from_ignorance) (see also [Russell's teapot](http://en.wikipedia.org/wiki/Russell%27s_teapot)). – lexicore Oct 31 '14 at 15:02
-
You leave poor Bertrand Russel and the (unrelated teapot) alone :-). I really see both sides of this, have a look at http://stackoverflow.com/questions/299068/how-slow-are-java-exceptions . It is possible that there is measurable difference but as we progress with Java versions, this is increasingly unlikely. – David Soroko Oct 31 '14 at 15:30
-
You're very likely to be right about the significant difference. From the other hand I think that private methods for instance may be handled differently than package/protected/public. I saw this [patent from IBM](http://www.google.com/patents/US8650537) (did not understood a word, to be honest). But, theoretically, wouldn't it be possible to inline the private method and optimize it taking in account the surrounding code? Such trick may give performance gain but is not possible with public or protected method as they could be overridden. So my feeling is, it is not so simple. – lexicore Oct 31 '14 at 15:41
-
@lexicore: The most popular (HotSpot) JVM is actually smarter than that: it will allow methods to be inlined if they aren't _actually_ overridden, even if they're public. If new classes are loaded that do override the method, the inlinings get deoptimized accordingly. – Louis Wasserman Oct 31 '14 at 15:48
-
I agree with all that. I think the point that is can be argued about is as follows: suppose that someone (say IBM) came up with a JVM in which private methods are in more efficient then otherwise identical public methods. I would say that his is a bug – David Soroko Oct 31 '14 at 15:51
-
@DavidSoroko I wouldn't even be surprized. Thank you people for the discussion. – lexicore Oct 31 '14 at 15:53