Question
How to create an immutable class

Answers

To create an immutable class following steps should be followed:

1.Create a final class.
2.Set the values of properties using constructor only.
3.Make the properties of the class final and private
4.Do not provide any setters for these properties.
5.If the instance fields include references to mutable objects, don't allow those objects to be changed:
1.Don't provide methods that modify the mutable objects.
2.Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

public final class FinalPersonClass
{
private final String name;
private final int age;
public FinalPersonClass(final String name, final int age)
{
this.name = name;
this.age = age;
}
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}   Your Comment




More Software,Java Questions..
What is the inputsplit in map reduce software?

Can variables be used in Java without initialization?

What is the difference between java bean and enterprise java bean?

What is software configuration management?

How does the java database connectivity (jdbc) work?

What are the advantages of threads in java?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

What is object cloning in Java?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?



Search
Can you Answer!!
  • Q What is the role of community resources in developing the communicative skills of students?
  • Q What is the function of the collagen fibers of the connective tissue?
  • Q Following is the syntax of the method to read a directory: fs.readdir(path, callback) Parameters Here is the description of the parameters used: path - This is the directory name including path. callback - This is the callback function which gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.
  • Q What is an antonym?
  • Q Who painted The Card Players, at Feb 2012 the most expensive artwork in the world?
  • Q What is a conversion constructor c++?
  • Q What is a meeting workspace?
  • Q Where is the Head Office of Syndicate Bank located?
  • Q A virtual image larger than the object can be formed by which mirror?
  • Q The only crator lake in India:
  • Q A screwdriver becomes a fuzzy screw when you add what