Search code examples
javascopedefaultprivate

private vs default scope for variables in java class - any performance issues


I have gone through numerous articles for the above question.

Main key points i can gather:

1.) Making private we mean only declared class has access.

2.) Nobody else outside this class can access these variables.

Mostly it happens, while creating a class we keep the variables scope as default, maybe just because of laziness to write extra word "private". We keep the fields with default scope only.

I want to understand how exactly this negligence from a developer causes issues , maybe

1.) Performance issues

2.) Garbage collection

3.) Compile time-runtime loading

... or any other parameters.


Solution

  • A developer that is too lazy to write the private keyword leaves the field open to be modified directly by code in other classes in the same package.

    Modifying a field directly from outside the code that knows how to correctly handle the field is troublesome, and can lead to inadvertent errors.

    Fields should generally be declared private, to prevent uncontrolled manipulation of the value. There are of course always exceptions to a rule like that, but all developer should write private without even thinking about it, and changing it to something else should require careful consideration.

    There is no performance impact. It is purely to guard against coding errors.