In C++, I can define an accessor member function that returns the value of (or reference to) a private data member, such that the caller cannot modify that private data member in any way.
Is there a way to do this in Java?
If so, how?
I know about the final
keyword but AFAIK, when applied to a method it:
- Prevents overriding/polymorphing that method in a subclass.
Makes that method inline-able.(see comment by @Joachim Sauer below)
But it doesn't restrict the method from returning a reference to a data member so that it can't modified by the caller.
Have I overlooked something obvious?