Is there in Java a difference in efficiency between a public or a private method when both called in the same class?
Asked
Active
Viewed 144 times
-1
-
3There's no difference. – Michael Sep 08 '17 at 08:18
-
5No. Padding the comment to make it sufficiently long. – Andy Turner Sep 08 '17 at 08:18
-
2Even if there were, I doubt this would be the most significant performance issue in your code. – Andy Turner Sep 08 '17 at 08:20
-
Are you noticing one? Did you benchmark your code? – OneCricketeer Sep 08 '17 at 08:22
1 Answers
2
No. There is no difference at all if you use it in the same class and you see the only difference while accessing it outside the class. Again there is no performance difference. It is just matter of giving access to method.
Access level modifiers determine whether other classes can use a particular field or invoke a particular method.
They do not impact any performance (as pointed by Andy in comments, unless you use nested classes and even then that's almost negligible difference ).

Suresh Atta
- 120,458
- 37
- 198
- 307
-
3I believe that there is a slight performance impact of declaring methods private in nested classes, and calling them from the outer class, as there are synthetic bridge methods generated to allow them to be called. But this doesn't apply in OP's situation, where the methods are called from the same class. – Andy Turner Sep 08 '17 at 08:26
-
@AndyTurner While true, I don't think they create considerable difference and OP doesn't mention about any nested classes. – Suresh Atta Sep 08 '17 at 08:34
-
Agreed. Just pointing out that there's a caveat to your last statement. – Andy Turner Sep 08 '17 at 08:36
-
1
-
In some corner cases the performance difference may be significant. See [this answer](https://stackoverflow.com/questions/23268120/private-method-inlining/23281131#23281131) – apangin Sep 08 '17 at 18:06