0

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);
}
Jackalope
  • 103
  • 1
  • 1
    That happens because the variable ``i`` is a copy of what you passed in, not the (original) variable ``num``. The reason for this is java using call-by-value for primitive types. – f1sh Apr 28 '16 at 13:57
  • Text would surely be getting updated outside function call as it a String object but num is int type , a primitive, it wont be updated outside function. – Vineet Kasat Apr 28 '16 at 13:58
  • @VineetKasat `String` will not be updated as well as `int`. – Sergii Bishyr Apr 28 '16 at 14:01
  • @SergheyBishyr You are right. String will also be not updated – Vineet Kasat Apr 29 '16 at 08:14

3 Answers3

2

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);
}
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
0

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”?

Community
  • 1
  • 1
Tangoo
  • 1,329
  • 4
  • 14
  • 34
0

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;
  }
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72