Search code examples
javauser-interfacemethodsglobal-variablesidentifier

How can I make a method return a "variable" (not value)?


I have a set of global double variables called softPrice0 (and 1, 2, 3)

The thing is I had the idea to use a method like this:

SOMEWORD getOPrice()  //I tried double, String, Object, variable, etc
{return softPrice0;}

So I can use it later like this: getOPrice()=5.8;

I know that using an array would do the trick but I would like to know if I can make methods throw variable names to use it as I explained.


thanks ortang

This is how I made it, the approach changed though.

setOPrice(Double.parseDouble(txtPriceDolar.getText())); //thats the call

void setOPrice(double value) { //this is the setter, no need of getter
switch(combobox.getSelectedIndex())
{case 1: this.softPrice0 = value; break;
 case 2: this.softPrice1 = value; break;
 case 3: this.softPrice2 = value; break;
default: this.softPrice3 = value; break;}}

now looks more simple, Thanks to everybody. Asking the wrong questions teaches a lot.


Solution

  • For setting you can not use the getter as you want to. getOPrecio()=5.8; will not work. You have to use a setter method. Take a look at the following sample, to access the value you have to use the getter(read) or setter(write).

    You would want to use something like setOPrecio(5.8).

    public class DoubleHolder {
      private double vaule;
    
      public double getValue() {
        return value;
      }
    
      public void setValue(double value) {
        this.value = value
      }
    }