You can create your own read-only class, and here's a simple example:
: Immutable1.java
//Objects that cannot is modified
//are immune to aliasing.
public class Immutable1 {
private int data;
Public Immutable1 (int initval) {
data = Initval;
}
public int Read () {return data;}
public Boolean nonzero () {return data!= 0;}
Public Immutable1 quadruple () {return
new Immutable1 (data * 4);
}
static void F (Immutable1 i1) {
Immutable1 quad = I1.quadruple ();
System.out.println ("I1 =" + I1.read ());
System.out.println ("quad =" + Quad.read ());
}
public static void Main (string[] args) {
Immutable1 x = new Immutable1 (a);
System.out.println ("x =" + X.read ());
f (x);
System.out.println ("x =" + X.read ());
}
} ///:~
All data is private, and you can see that there is no public way to make changes to the data. In fact, the way you really need to modify an object is quadruple (), but it does this by creating a new Immutable1 object with the original object intact.
Method F () takes a Immutable1 object and takes a different action on it, and the output of main () shows no changes to X. Therefore, the X object can be processed many times without causing any harm because it guarantees that the object will not be altered according to the design of the Immutable1 class.