java - Objects in an immutable class as member variable -


I have a class X which keeps the employee object I want to make Class X ineffective. Even if I declare the purpose of the employee as a final form, then the employee class is freely available and people can reach the setter methods of that particular employee. Can it be an ordinary class employee and another unchanging class X, which keeps employees, but once the employee is handed it, the value of the employee can not be changed?

  Public Last Square X {Private End Employee Employee; Public X (employee E) {this.emp = e; } Public Employee getEmp () {return emp; }}  

Employees, I do not want to make it irreversible, just want to make Class X unchanged. After assigning an employee, it should not be allowed to change it. Is it possible that, without making the class employee irreversible, I want to be able to be freely available to the class employee, so it does not want to privatize my own construction.

You do not have to expose the employee member of the immutable class. You can introduce greater methods that will highlight only relevant areas of the employee.

  Public Last Square X {Private End Employee Employee; Public X (employee E) {this.emp = e.clone (); } Public string getEmpName () {return emp.getName (); } Public int getEmpSalary () {return emp.getSalary (); Surely, there is one more thing to make sure that the class is actually immutable, the constructor should make a copy of the employee passed to it. Otherwise, the manufacturer's caller will still have a reference to the employee, and he will be able to change the object. 


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -