Pass Enum values by reference in Java

Title might be little misleading since in java we pass always by value, but we can mimic passing by reference.

/**
 * This class allows us to pass values by reference ranter than value.
 * Not directly as java just passes reference of the object by value but indirecly 
 * @author Greg
 *
 * @param <E>
 */
public class IndirectReference<E> {
	public E ref;
 
	public IndirectReference(E ref) {
		this.ref = ref;
	}
 
	public void set(E ref) {
		this.ref = ref;
	}
}

Example usage

Just a snippet copied from my interpreter

	IndirectReference<ControlSignal> signal = new IndirectReference<ControlSignal>(ControlSignal.NOOP);
	if(signal.ref == ControlSignal.BREAK){
	    break;
	}

Leave a Comment

Your email address will not be published. Required fields are marked *