Skip to main content

Memory optimization of public methods in java [Resolved]

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.

Question Credit: Otix avi
Question Reference
Asked June 12, 2019
Posted Under: Programming
2 Answers

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.

credit: Kilian Foth
Answered June 12, 2019
Your Answer