2

In preparing for an interview, someone mentioned knowing how to make a class/method in java read-only. I've been doing a bit of searching, but haven't found anything really concrete.

Maybe this question is simpler to answer than I'm making it out to be, but what would be a way to make a class or method read-only in java?

joshft91
  • 1,755
  • 7
  • 38
  • 53

5 Answers5

0

The following code will ensure that your class is always READ ONLY, but if you find any loop hole, please post it here.

import java.io.Serializable;

final public class ImmutableClass implements Cloneable,Serializable {
    private static final long serialVersionUID = 6488148163144293060L;
    private static volatile ImmutableClass instance;

    private ImmutableClass() {
        // no-op
        System.out.println("instance created : " + this.hashCode());
    }

    /**
     * Lazy Instantiation
     * 
     * @return
     */
    public static ImmutableClass getInstance() {
        if (instance == null) {
            synchronized (ImmutableClass.class) {
                System.out.println("aquired lock");
                if (instance == null) {
                    instance = new ImmutableClass() {
                    };
                }
                System.out.println("released lock");
            }
        }
        return instance;
    }

    public Object readResolve() {
        System.out.println("readResolve()");
        return getInstance();
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

The Read-only class means, we are talking about "IMMUTABLE" concept.

The following example depicts the same:

public class ImmutableString { 
    static String upcase(String s) { 
        return s.toUpperCase(); // here local variable s vanishes 
                // it return the value to a new String object 
    } 

    public static void main(String[] args) { 
        String s = new String("abc");  
        System.out.println(s); //abc 

        String s1 = upcase(s);  

        System.out.println(s1); //ABC 
        System.out.println(s); //abc 
    } 
}  
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
0

Lets Say you want a read only version of an object,

case 1: if your class contains fields which are not pointers to any other objects eg:

public class Person{
private String name;
//Getters n Setters
}

in this case, you can return a copy of this class, write a constructor that accepts Person, any one who wants to get a Person object will have a copy of this object so any Setter operations wont effect the original object(Strings are immutable)

Case 2: in case your object contains a pointer to another object or list or map

in this case make classes implement an interface which has only read-only methods(Getters) and wherever you are returning the object, change it to return this interface, so client will have access to only read-only methods

eg:

class Person implements ReadOnly{
String name;
.. assume pointers also in here
// Getter n Setters 

 public PersonReadOnly(){
  return this;
}
}

interface PersonReadOnly {
public String getName();
}
jayant mishra
  • 686
  • 5
  • 13
  • The second state has some issues. One can cast the returned object (which implements the ReadOnly interface) to the Person. After that all setter methods are available. – hadi.mansouri Jan 24 '19 at 12:44
  • I think to improve second state: we could extend another class from Preson and make setter methods of that as an empty method which do nothing or throw an exception when one calls one of setter methods. – hadi.mansouri Jan 24 '19 at 12:46
0

Something is read-only if it cannot be modified (or, in a weaker sense, cannot be observed to have been modified). This is a really useful property that goes with a lot of different names: read-only, immutable, finality, const-ness, observer, purity, and probably others.

Why it is useful

  • if you know that there is no possibility of others changing something, there are a lot less downsides to giving access or sharing it.
  • if a whole object is immutable, there is no longer that much of a difference between sharing the original and sharing copies. This may save a lot of space by avoiding copies.
  • immutable things never break when several threads are accessing them at once, because all threads will see the same values.
  • it is a lot easier to reason about how things change when there is a very small set of places in which they can change. Immutability helps you keep that set small.

How to get immutability in classes and methods in Java

First, let us assume that your class can only be modified via access to its methods (= no public attributes, and no use of reflection to gain access to privates)

  • a class is immutable if no methods can modify its state; that is, no setters, and all other methods can only read state and/or copy state to a new object. For example, the JDK's BigDecimal are immutable: a.add(b) returns a new BigDecimal, but does not modify either a or `b.
  • a method can be said to be read only, or const (especially in the C++ world), or observer, when it is guaranteed not to change the sate of an object. Some languages, most notably C++, have a keyword for this (const). In others (Java) it is up to the documentation to clarify whether or not the internal state will change.

Other types of immutability in Java

  • static methods have no underlying object, and cannot modify what they do not have. It does not make much sense to call them immutable, though.
  • final next to a class declaration prevents subclassing of classes. It does not make them immutable, but it does make it harder to (by subclassing) break the immutability of a class.
  • final next to an attribute declaration ensures that the attribute's value cannot be changed once initialized. This allows you to make those attributes public without compromising immutability, for example. A better use would be to make it abundantly clear, in an immutable class, that there will be no modifications to those attributes.
  • enum in Java builds special classes that have only a given set of members, a set which cannot be modified at run-time. It is a sort of generalization of the concept of singletons.
tucuxi
  • 17,561
  • 2
  • 43
  • 74
-1

Simple rule: Don't have any public fields and No public setter methods.

For example, see class below:

final class AReadOnlyClass
{
    private int anInt;

    public int GetAnInt()
    {
        return anInt;
    }
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • final doesn't mean readonly (except primitive types). Value of objects referenced by final variable can still be changed. – kosa Jan 10 '13 at 04:44
  • yes, agree. but reference will point to same object always. in that view it is final. – Azodious Jan 10 '13 at 04:46
  • 2
    `final` has nothing to do with immutability. Your class is a good example, just get rid of the `final` stuff in your answer. – Brian Roach Jan 10 '13 at 04:53
  • As @BrianRoach commented, final in your example tells this class can't be extended, that doesn't mean it can't be instantiated. – kosa Jan 10 '13 at 04:54
  • @Nambari, BrianRoach: by making it final we need not worry about changing method behaviour by overriding certain method. that's why i marked it `final`. – Azodious Jan 10 '13 at 05:01
  • I mean the part where you talk about public fields being `final` - that doesn't prevent someone from mutating *that* object. Everything should be private and in all honestly if you're talking immutability you should be returning copies rather than references to internal, private fields. (as your example does because it's a primitive) – Brian Roach Jan 10 '13 at 05:11