Ankur Singhal
Ankur Singhal

Reputation: 26077

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.

Upvotes: 0

Views: 973

Answers (4)

Tirath
Tirath

Reputation: 2404

It has to do with encapsulation, coupling, open for extension but closed for modification principles.

For example - if I'm writing a library class and have a field which has public\ default access. Now, once I publish my library and clients of my library starts using it - I have no way to go back and change this field which is accessible to client code as there is no way to make sure - it's not used or changing it will not break clients code.

So, am left with only option of supporting it the same way. Had this been private and I had used getters and setters - I can always do/ add a little extra in future versions.

Upvotes: 0

Anand Vaidya
Anand Vaidya

Reputation: 1461

I don't think it makes any difference on performance. It will have some impact on structure of the code, and too many default variables re not recommended without a strong motive to do so.

From garbage collection and class loading point of view, they are done based on its need. if JVM thinks that the object is needed, then it might remain in the memory. Although there is no direct impact of default access on garbage collection, there is a high probability of programming error that can cause a possible memory leak.

Upvotes: 1

Andreas
Andreas

Reputation: 159155

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.

Upvotes: 1

Harrison
Harrison

Reputation: 114

Private vs Public/etc declarations are handled the same by the java interpreter and compiler. They're just conventions that we use to keep our code modularized, but are handled exactly the same in memory space.

Upvotes: 1

Related Questions