Immutable mu = new Immutable(); mu.state[1] = 'o';
public class Sharable { private Sharable() { } public static Immutable getImmutableInstance(int a, int b) { return new Immutable(a, b); } }
Map<String, Immutable> _vars = new HashMap<String, Immutable>(); public void modify(String key, int v) { _vars.put(key, new Immutable(v); }
Immutable oldObj = new Immutable(...); Immutable newObj = new Immutable(oldObj.property1, "a new value for property 2");
private static Immutable immutable = new Immutable(0); public static void main(String[] args) { IntStream.range(0,10).forEach(i -> { Immutable localImmutable = immutable.changeState(i); System.out.println(localImmutable.toString()); }); }
new Immutable(one, fish, two, fish, red, fish, blue, fish); /*1 */ params = new ImmutableParameters(); /*2 */ params.setType("fowl"); new Immutable(params); factory = new ImmutableFactory(); /*3 */ factory.setType("fish"); factory.getInstance(); Immutable boringImmutable = new Immutable(); /* 4 */ Immutable lessBoring = boringImmutable.setType("vegetable");
private static Object newInstance(Class cls) { if(log.isTraceEnabled()) { log.trace("new " + cls.getName()); } Object instance; try { Constructor ctor = cls.getConstructor(null); instance = ctor.newInstance(null); } catch(NoSuchMethodException e) { log.warn("No no-arg constructor in " + cls); instance = new Immutable(cls); } catch(Exception e) { throw new IllegalStateException("Failed to create an instance of " + cls + " with the no-arg constructor: " + e.getMessage() ); } return instance; }
private static Immutable immutable = new Immutable(0); public static void main(String[] args) { IntStream.range(0,10).forEach(i -> { immutable = immutable.changeState(i); System.out.println(immutable.toString()); }); }