Vinay Sharma
Vinay Sharma

Reputation: 1303

How we can get Boolean value updated through other method in main method

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.

Upvotes: 3

Views: 10996

Answers (5)

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 
     }
}

Upvotes: 1

AlexR
AlexR

Reputation: 115328

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
}

Upvotes: 7

JVidiri
JVidiri

Reputation: 23

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.

Upvotes: 0

user3398633
user3398633

Reputation:

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;

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

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

Upvotes: 0

Related Questions