I am looking for a clean and safe way to ensure tha a field of a class will never be set to null. I would like the field value to be set once in the class constructor and never modified later. I think that he readonly keyword in C# allows this. Is there a way to do the same in Java?
class foo
{
private Object bar;
public foo(Object pBar)
{
if(pBar == null)
{
bar = new Object();
}
else
{
bar = pBar
}
}
// I DO NOT WANT ANYONE TO MODIFY THE VALUE OF bar OUT OF THE CONSTRUCTOR
}