-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learn Java with Projects
By :

In the API, there are mutable types, such as StringBuilder
and ArrayList
, and immutable types, such as String
and Integer
. When something is “immutable,” it means it cannot change. We can use the final
keyword to make a primitive value immutable. When we apply final
to a reference, it is the reference that is immutable and not the object.
What if we wanted to create our own type (class) and make it immutable? In other words, we want the objects based on our custom class to be immutable. What considerations are involved? That is what we’ll discuss in this section.
Before we present the checklist, recall that Java uses call by value when passing arguments to, and retrieving values from, methods. Call by value implies that a copy of the argument is made and that the method works with that copy. For primitives, this means that the called method cannot change the primitive value passed from the caller method. This is analogous...