Does it optimize a program in java to declare methods private wherever they can be private or it doesn't change a thing and it's just a question of encapsulation? I was asking myself if declaring a method public was heavier than a declaring it private in terms of memory consumption.

  • 8
    What actual problem are you trying to solve here? – Philip Kendall Jun 11 at 16:23
  • 3
    In Java your method declarations only use a tiny bit of memory in the meta (or permanent) space. Even if this was the case, it's irrelevant unless you have some insane number of class definitions. – JimmyJames Jun 11 at 16:38
  • 1
    Possible duplicate of Is micro-optimisation important when coding? – Doc Brown Jun 12 at 5:15

private methods can never be overridden, whereas protected and public methods can be overridden.

As a consequence of this, the underlying runtime knows that for private methods:

  • There is no need to place them in the method table
  • It can invoke them using non-virtual dispatching

By contrast, protected and public methods are most likely placed into the method table, and, analysis would take place to determine whether to invoke using virtual dispatch through the method table or not.  (To err on the side of correctness, virtual dispatch would be used.  Due to dynamic class loading the method may potentially be overridden sometime in the future, which might require adjustment to optimistic choices.)

The above does not take into account final classes: methods in final classes share the above discussed qualities among all methods private, protected, and public.

But worrying about this is micro optimization. It is very unlikely that virtual dispatch will have a meaningful effect on the performance or memory consumption of your application.

  • In a hot loop, it can have a very noticeable impact on the performance. This is the difference between monomorphic and megamorphic calls that prevent JIT from doing method inlining and other optimizations. – Juraj Martinka Jun 14 at 5:30
  • @JurajMartinka: The JVM supports speculative inlining. That is, the mere use of a protected or public modifier does not make a call site megamorphic, or prevent inlining or other optimizations. – meriton Jun 15 at 18:35
  • @meriton that is correct. What makes a callsite megamorphic isn't actually a number of different "subtypes" present int the program code or dependencies but what JIT observes at runtime. I was commenting on "It is very unlikely that virtual dispatch will have a meaningful effect on the performance or memory consumption of your application." which is imho false. – Juraj Martinka Jun 16 at 19:39

Access modifiers like private and public have no effect on the resource requirements of a Java program. They exist largely as documentation and aids to understanding for human readers and writers of the code. Even when information about the status of a field or method is retained at runtime, it's likely to be stored in a bitfield-like structure, so either value takes the same amount of memory.

In short, making things private does not optimize computer memory (RAM) in any way. It can, however, be better for human memory, because an API with fewer visible members is easier to understand and easier to remember than an API with everything public.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.