How to make a class immutable?

Answer Posted / cm

1. Mark the class as 'final' to avoid any
subclassing/overriding
2. Make the fields as 'private' & 'final' to avoid
reassining in any way after creation of the object by
passing params via contructor

Is This Answer Correct ?    17 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is callablestatement? How you can call stored procedure to pass in parameter?

535


What is the purpose of the runtime class in java programming?

553


What is number data type in java?

544


Why enumeration is faster than iterator?

522


Explain the importance of finally block in java?

562






What is set string?

584


FOR EXAMPLE WE R HAVING TWO LIST ELEMENTS ..BOTH LISTS CONTAINS ID,NAME,PLACE ..I NEED TO COMPARE BOTH IDS IN TWO LISTS,IF ID'S R SAME MEANS WE HAVE ADD THE DETAILS(LIKE NAME,PLACE) TO MAP...HOW IS POSSIBLE ?CAN ANY ONE SUGGEST?

2620


What is native method in java?

554


How do you get the length of a string in java?

514


What is method overriding in java ?

646


What are the differences between include directive and include action?

534


describe method overloading

527


What is ‘has a’’ relationship in java?

714


What is the exact difference in between Unicast and Multicast object?

1500


Is it possible to write a regular expression to check if string is a number?

534