-1

Is there in Java a difference in efficiency between a public or a private method when both called in the same class?

Misi
  • 104
  • 7

1 Answers1

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
  • 3
    I 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
    @AndyTurner Gotcha. Added your feedback in answer :) – Suresh Atta Sep 08 '17 at 08:39
  • 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