How can i pass enum parameter by reference in java? Any solution?
Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
enums are technically descendants of Enum class. So, if you want to use only Enum's standard methods in your method (such as values()), pass the parameter like this: static void printEnumValue(Enum generalInformation) See, that the only thing you have to change is the capital letter E.
Inheritance Is Not Allowed for Enums.
By default enums have their own string values, we can also assign some custom values to enums.
In Java you cannot pass any parameters by reference.
The only workaround I can think of would be to create a wrapper class, and wrap an enum.
public class EnumReference {
public YourEnumType ref;
}
And then you would use it like so:
public void someMethod(EnumReference reference) {
reference.ref = YourEnumType.Something;
}
Now, reference will contain a different value for your enum; essentially mimicking pass-by-reference.
Java is pass by value - always.
You pass references, not objects. References are passed by value.
You can change the state of a mutable object that the reference points to in a function that it is passed into, but you cannot change the reference itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With