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.
Java passes by value, so this isn't possible without a separate getter and setter.
Examples:
void setOPrecio(double softPrecio0) {
this.softPrecio0 = softPrecio0;
}
double getOPrecio() {
return softPrecio0;
}
However, if the value is a class, you may be looking for something along the lines of the singleton pattern.
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
Singleton example code from Wikipedia's article .
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
}
}
Java has no way of passing or returning a "variable".
The closest you are going to get are:
passing or returning an object whose fields are the "variables", or
passing or returning an array whose elements could be viewed as "variables".
And to be clear, neither of the contrivances are close to passing or returning a bare variable.
You need to rethink your problem / solution in terms of the constructs that Java does provide.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.