How do I create a reference to a constant object?
final Myclass obj = new Myclass();
does not work, it says obj(the reference) should not be re-assigned but we can still change the object referred.
I want to ensure that the object itself does not change once constructed.
Just make it immutable (like String
is). Or wrap it in another object which restricts access to mutators of the object in question (like Collections.unmodifiableList()
and consorts do).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With