Question

From the effective Java book it states that "An object can always be reused if it is immutable".

String s = "shane";
String p = "shane";

This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to contain the same string literal.

What about the below final class which is also immutable?. Can the Point Object be re-used?.

public final class Point {
  private final int x, y;

  public Point(int x, int y) {
   this.x = x;
   this.y = y;
  }

  public int getX() { return x; }
  public int getY() { return y;
 }

Can anyone provide me an example of the above immutable class where its object/instance can be re-used?. I am just confused on how the re-usability would occur?.

I am able to relate with String and Integer Classes, but not with user defined classes.

Was it helpful?

Solution

It "can" be reused, in that you could use the same object in multiple places and it would be fine. But it won't be, automatically. The JVM itself manges reuse Integer objects for the range -128 - 127

Integers caching in Java

"intern"ed strings (including literals) similarly are managed by the JVM. The closest to automatic reuse you could have here would be to make the constructor private, and create a factory method:

 Point.create(int x, int y)

And have the implementation maintain a cache of objects that you'd like to reuse (like Integers effectively cache -128 to 127) But you'll have to do the work yourself.

Edit:

You'd basically have:

 private static final Map<Pair<Integer, Integer>, Point> cache = new HashMap<>();

 public Point create(int x, int y) {
   Pair<Integer, Integer> key = Pair.of(x, y);
   if (cache.containsKey(key)) {
     return cache.get(key);
   }
   Point p = new Point(x, y);
   cache.put(key, p);
   return p;
 }

Edit: Alternatively, add hashCode() and equals() to the Point class, and just use a HashSet. Would be simpler.

OTHER TIPS

Re usable simply means to change the "reference" variable value. e.g. an int is can be reused and its value changed a data type is a little different the reference variable is re-initiated for example using the "new" instane e.g. myframe=new JFrame() variables declared "final" are a "constant" and are mutable.

The class above itself requires its reference variable at initiation to be declared "final" to be mutable although its contents is effectively mutable, the difficulty is the definition of context of which (variable or class definition) part is the mutable.

Immutability means when an object is created its state at the creation time is going to stay through out its life. And yes, the class you showed and object of that class is immutable, as you are initialing states in constructor and there are no setters.

About the re-use: yes you can reuse the same object over and over where an object of type Point is required, but for that purpose you have to hold on to an object once it's created for that. As @James suggested, you can use a factory for object creation and that factory can decide if it needs to create a new object or use an existing one when you ask for a Point object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top