Anmol Aggarwal
Anmol Aggarwal

Reputation: 1

Creating variable alias in java

I need a way to write R1 or R[1] and refer to the same variable.

Upvotes: 0

Views: 623

Answers (1)

Larry OBrien
Larry OBrien

Reputation: 8606

If you're trying to use the general pattern of having ALPHANUMBER and ALPHA[NUMBER] refer to the same variable, you can't do it. The square bracket notation in Java refers to an element in an array, while the first is just a general identifier that can refer to a variable.

In the specific case of creating two variables, one a direct reference and the other an element in an array, you can simply point r[1] = r1 (as long as r1 has been initialized).

Upvotes: 1

Related Questions