-2
package test;

public class Main {
    static double a,b,c,d;

    public static void changeValue(double x, double y)
    {
        x=1;
        y=2;
    }

    public static void main (String args[])
    {
        changeValue(a,b);
        changeValue(c,d);
        System.out.println("a="+a+"\nb="+b+"\nc="+c+"\nd="+d);
    }

}

a,b,c,d values do not change.

How to change the method parameters?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Tedd
  • 27
  • 3
  • 2
    Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Tom Oct 17 '15 at 09:19

5 Answers5

2

Since Java is pass by value (primitves and Object references are passed as copies),the changes you make inside the method will not be reflected from where it has been invoked

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • So isn't there any method I can change those variables without giving each of them a value, like a=1,b=2,c=1,d=2 ? – Tedd Oct 17 '15 at 09:17
  • 1
    @Tedd Changing a variable means assigning a value to them. How do you intend to wash your hands without getting them wet? And if you ask somebody else to wash your hands, but that person puts HIS hands under the water ... nothing will change at your end. That is what pass by value means in this case. – GhostCat Oct 17 '15 at 10:09
0

In short you can't change since Java is always pass-by-value. However you can use the wrapper. This wil work:

class DoubleHolder { public double value = 0; }

class Ideone
{


   static DoubleHolder a = new DoubleHolder();
   static DoubleHolder b = new DoubleHolder();

    public static void changeValue(DoubleHolder x, DoubleHolder y)
    {
        x.value=1;
        y.value=2;
    }

    public static void main (String args[])
    {

        changeValue(a,b);

        System.out.println("a="+a.value+"\nb="+b.value+"\n");
    }


}

You can enhance it to use get/set, of course.

Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18
0

Java supports pass by value. So when it passes a,b,c,d to changeValue() method it copies those variable's values to x , y. So if you change the value of x and y it will not affect a,b,c,d

Rahman
  • 3,755
  • 3
  • 26
  • 43
0

With two arguments you can't know what variables you change

changeValue(a,b);
changeValue(c,d);

won't work even if you pass the values to the method. Note, that you pass parameters by value, not by reference. But if you change the signature to

changeValue(double x, double y, double u, double w)

then you know how to initialize the class variables

public class Main {
    static double a,b,c,d;

    public static void changeValue(double x, double y, double u, double w)
    {
        a = x;
        b = y;
        c = u;
        d = w; 
    } 

    public static void main (String args[])
    {
        changeValue(1,2,3,4);
        System.out.println("a="+a+"\nb="+b+"\nc="+c+"\nd="+d);
    }

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

You can use OO programmng:

package test;

public class Main {
    private double a,b,c,d;

    public Main() {
        a = 0.0;
        b = 0.0;
        c = 0.0;
        d = 0.0;
    }
    public void setA(double x) {
        a=x;
    }
    public void setB(double x) {
        b=x;
    }

    public void setC(double x) {
        c=x;
    }

    public void setD(double x) {
        d=x;
    }

    public double getA() {
        return a;
    }
    public double getB() {
        return b;
    }
    public double getC() {
        return c;
    }
    public double getD() {
        return d;
    }


    public static void main (String args[]) {
        Main myMain = new Main();
        myMain.setA(1);
        myMain.setB(2);
        myMain.setC(1);
        myMain.setD(2);

        System.out.println("a="+myMain.getA()+"\nb="+myMain.getB()+"\nc="+myMain.getC()+"\nd="+myMain.getD());
    }
}

This way you don't have to change them by pairs, it's a more flexible solution in case you want to change them individually.

Eduardo Yáñez Parareda
  • 9,126
  • 4
  • 37
  • 50