However, the steps above are the basis of an Immutable Class. NOTE: Not all classes documented as “immutable” follow these rules. As an added Task, handle the case where exits is null when passed to the constructor. Don’t share references to the mutable objects. If the instance fields include references to mutable objects, don’t allow those objects to be changed: - Don’t provide methods that modify the mutable objects. The strategy for creating an Immutable Class is: Steps: 1. Iterator public final class FinalClassExample Make the Location class an Immutable Class. The code in the main method of the FinalClassExample.java file tests the immutability of the object.Ĭreate a new file called FinalClassExample.java and copy in the following code:įinalClassExample.java import java. The FinalClassExample class defines the fields and provides the constructor method that uses deep copy to initialize the object. The following class is an example that illustrates the basics of immutability.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |