Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of #define in Java for macros

My question is close to this one but not quite the same.

I have an inherited (as in, I can't/won't change it) array of parameters in my class like so:

public double[] params;

The class utilises these parameters in complex ways, so I would prefer to have human-readable names for each element in the array. In C, you would do something like this

#define MY_READABLE_PARAMETER params[0]

I am also aware that in Java I could create a bunch of constants or an enumerator with attributes. Then, to access a parameter I'd have to type something like this:

params[MY_READABLE_PARAMETER]

This is acceptable but I would really like to omit the array name altogether. Is it possible?

like image 410
Naurgul Avatar asked Jan 24 '26 22:01

Naurgul


2 Answers

Yes, it is possible simply by not using an array:

double myReadableParameter;
double anotherReadableParameter;

If you need to access them as a collection, you can always put them in a list.

like image 99
Karel Petranek Avatar answered Jan 26 '26 12:01

Karel Petranek


Is there any reason you couldn't do this?

...
public double getMyReadableParam() {
    return params[0];
}

public void setMyReadableParam(double value) {
    params[0] = value;
}
...
like image 38
Ben Avatar answered Jan 26 '26 12:01

Ben