2

Please ignore formatting and sentence related issues.

class ABC 
{   
 public void change(Boolean x, Boolean y, StringBuffer s)
 {
    x=true;
    y=true;
    s.append("vinay");
 }

 public static void main(String a[])
 {
    Boolean x = false;
    Boolean y = false;
    x=false;
    y=false;
    StringBuffer s = new StringBuffer();
    s.append("jasi");
    ABC p= new ABC();
    p.change(x,y,s);

    System.out.println(x);
    System.out.println(y);
    System.out.println(s);      
 }
}

i want to get all changes which i made in change() method in main() method for Boolean x,y as we are getting s modified in main function. Is there any way by which we can get modified value in main method.

Vinay Sharma
  • 1,163
  • 1
  • 10
  • 20

5 Answers5

6

Java passes arguments by value, so all changes done in your change() method are not visible for caller.

In order to do what you want you can either: 1. define this variable as class members. 2. return them as a return value of the method. Yes, you are limited by only one return value but if your want to can create array of booleans or create specal class that contains both. 3. You can pass to method mutable container that contains boolean. One of the ways is to use AtomicBoolean for this:

public void change(AtomicBoolean x, AtomicBoolean y, StringBuffer s) {
    x.set(true);
    y.set(true);
    s.append("vinay");
}

public static void main(String a[]) {
    AtomicBoolean x = new AtomicBoolean(false);
    Boolean y =  = new AtomicBoolean(false);
    change(x, y);
    System.out.println(x.get() + ", " + y.get()); // will print true true
}
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • sir but we are getting updated value in case of StringBuffer object s ? – Vinay Sharma Jun 17 '14 at 19:45
  • That's because a StringBuffer object can be modified. With the reference, you can change it. But the StringBuffer reference parameter is pass-by-value. That means if you make s point to a different object, it will still point to the original object outside the change() function. – Edwin Torres Jun 17 '14 at 19:52
  • what do you mean by mutable container that contains boolean – Vinay Sharma Jun 17 '14 at 20:01
1

Options:

  1. Place the variables outside of the two methods (as class variables)
  2. Create an array (there for referencing an address instead of a value).
  3. Create a new Object that contains these two Boolean values (create a new class or use an ArrayList/HashMap/etc)
  4. AtomicBoolean

Option 1:

class ABC 
{   
    Boolean x = false;
    Boolean y = false;
    public void change(StringBuffer s)
    {
        //code
    }

     public static void main(String a[])
     {
         //code
         p.change(s);
         //code 
     }
}

Option 2:

class ABC 
{   
    public void change(Boolean b, StringBuffer s)
    {
         b[0] = true;
         b[1] = true;
        //code
    }

     public static void main(String a[])
     {
         Boolean[] b = new Boolean[2];
         b[0] = false;
         b[1] = false;
         //code
         p.change(b, s);
         //code 
     }
}
Community
  • 1
  • 1
0

Java is pass-by-value so when you pass the boolean here p.change(x,y,s); it will treat as pass by value and when you initialized the boolean from the change method

x=true;
y=true;

it wont grab that reference but will get destroyed when it is out of scope

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

You can transform your boolean as static var, you'll call them with ABC.x and define just after the class with public static boolean x; It'll looks like

class ABC 
{
    public static boolean x;
0

In Java the arguments are passed by value, not as pointers, so the variables that you passed to the function have their values copied to local variables in the method, so all the changes only affects that local copy.

what you can do is get global variables, or get a return to your function.

att.

JVidiri
  • 23
  • 7