Before I get on with the question, in all my searches I cannot seem to find this, so I am sorry if it is a dupe. Now to the question:
Is there a modifier to make a variable visible but not modifiable outside of it's class without changing it's modifiable inside it's class? So basically to other classes it would be a public final
but to the class it resides in it's just public
. I found this and this, but neither one quite answers my question.
Again, sorry if this is a duplicate question and thanks ahead of time.
Is there a modifier to make a variable visible but not modifiable outside of it's class
Make it private and expose only getter method for other classes to access. If its public then you can't stop it to make modifiable by other class. If a class can access it then it can modify it also.
Try to avoid using public modifies for instance variables. Always access it using public getter & setter methods that give you more control.
For e.g. in setter method you can check for the validity of the passed value.
Only two access modifiers is allowed, public and no modifier
All the four public, private, protected and no modifer is allowed.
For better understanding, member level access is formulated as a table: