Jackalope
Jackalope

Reputation: 103

How can I change the value of a variable from inside a method?

I am trying to create a method to increase the value of various variables, here is an example of the type of logic i'm currently using, however when the method is finished the original variable has not changed. What do I need to add or replace to allow the value to change outside the method?

static int num = 2;
static String text = "3";

public static void up(int i, String s){
    //Debug
    System.out.println("Before Change");
    System.out.println("I: " + i);
    System.out.println("S: " + s);
    System.out.println("Num: " + num);
    System.out.println("Text: " + text);

    //Code
    i = i + 3;
    s = String.valueOf(i);

    //Debug
    System.out.println("After Change");
    System.out.println("I: " + i);
    System.out.println("S: " + s);
    System.out.println("Num: " + num);
    System.out.println("Text: " + text);              
}

public static void main(String[] args) {       
    up(num, text);

    //Debug
    System.out.println("Out of Scope");
    System.out.println("Num: " + num);
    System.out.println("Text: " + text);
}

Upvotes: 0

Views: 181

Answers (3)

You can do it with a WrapperClass. This is because Java uses Pass By Value as others mentioned. The workaround is to create WrapperClass if there are multiple values. Here is how you can modify the class using WrapperClass. When working with Corba Java frameworks provide Holder Classes to give the reference semantics.

  static int num = 2;
  static String text = "3";

  public static void up(WrapperClass w){
      //Debug
      System.out.println("Before Change");
      System.out.println("I: " + w.i);
      System.out.println("S: " + w.s);
      System.out.println("Num: " + num);
      System.out.println("Text: " + text);

      //Code
      w.i = w.i + 3;
      w.s = String.valueOf(w.i);

      //Debug
      System.out.println("After Change");
      System.out.println("I: " + w.i);
      System.out.println("S: " + w.s);
      System.out.println("Num: " + num);
      System.out.println("Text: " + text);
  }

  public static void main(String[] args) {
      WrapperClass w = new WrapperClass();
      w.i = num;
      w.s = text;
      up(w);

      //Debug
      System.out.println("Out of Scope");
      System.out.println("Num: " + w.i);
      System.out.println("Text: " + w.s);
  }


  static class WrapperClass {
    public int i;
    public String s;
  }

Upvotes: 0

Guillaume Barré
Guillaume Barré

Reputation: 4228

The int i and the String s you are passing to your function are passed by value. It means that you only receive a copy of the variable. An action on the variable won't affect its original value.

You can modify you method and make it return an object containing the modified values:

Create a new class to encapsulate the modified values :

class Result{
    int i;
    String s;
    public Result(int i, String s){
        this.i = i;
        this.s = s;
    }
}

Now your method can return this Result

public static Result up(int i, String s){
    //Code
    i = i + 3;
    s = String.valueOf(i);
    return new Result(i,  s);
}

You can then have access to the modified values in your main method:

public static void main(String[] args) {   
    Result r = up(num, "test");
    System.out.println("int result " + r.i);
    System.out.println("string result " + r.s);
}

Upvotes: 2

Tangoo
Tangoo

Reputation: 1449

You just pass the copy-value of the variables i when call method up(int i, String s).It will not change the value in method up.

Read this post:Is Java “pass-by-reference” or “pass-by-value”?

Upvotes: 0

Related Questions