1

I would like to know why if I set eq = false in the secondPart method then change it to true in the removeAccess method(for the System.out.println shows that eq = true) but then back in the secondPart() the System.out.println shows that eq = false?

 public void secondPart(){
    boolean eq = false; 
    removeAccess(copy,d,eq);
    System.out.println(eq);
 }


private void removeAccess(List<Integer> copy, Integer letterNum,boolean eq){
for(int i =0; i<copy.size(); i++){
        System.out.println("LetterNum:" + letterNum);
        System.out.println("Copy:" + copy.get(i));
        if(letterNum == copy.get(i)){

            copy.remove(letterNum);
            eq = true;
            break;
        }

        else{
            eq = false;

        }
    }

    System.out.println("Eq:" + eq);


}
Rika
  • 768
  • 1
  • 5
  • 16
  • 1
    because parameters are passed by value in Java – Sleiman Jneidi Nov 07 '14 at 18:57
  • FIY: `letterNum` is an `Integer` not an `int`. `Integer` should be compared by using `equals` instead of `==`. For example `if(letterNum.equals(copy.get(i)))`. `==` normally compares either the value (if the variables are primitives like `int`) or the reference when they are `Objects`. – GameDroids Nov 07 '14 at 19:06
  • @SleimanJneidi everybody's answer was fine but I like yours the best for it really answered my question of why it won't work. Thanks – Rika Nov 07 '14 at 19:47
  • @Rika I will post it as an answer then – Sleiman Jneidi Nov 07 '14 at 19:51

6 Answers6

3

In removeAccess, your eq variable is a copy of the eq from main. Changing removeAccess's eq does not change eq in main.

If you want the value eq from removeAccess, then you must return it. There is no need to pass it as a parameter; remove the boolean parameter.

private boolean removeAccess(List<Integer> copy, Integer letterNum) {
    boolean eq = false;
    // rest of code from your method remains the same
    return eq;
}

Then in main:

eq = removeAccess(copy,d);
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

boolean is not a class but a primitive type. In your function call you hand over the value false not a reference to the object. Changing the value of paramenter in the function has no effect on the original boolean.

openCage
  • 2,735
  • 1
  • 18
  • 24
  • It would make no difference if `eq` were an object. It would enable changing of properties of the instance by invoking methods on it, but the `eq` inside the method would still be a *copy* of the reference: assigning to `eq` (e.g., `eq = null`) would have no effect outside the method. – chiastic-security Nov 07 '14 at 19:21
1

You're trying to achieve pass by reference: you want eq in your method to refer to the same boolean that was passed in. But Java has no pass by reference. It only has pass by value: the value of eq is copied into a new variable that is discarded after the method finishes executing.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
1

He just wanted to know why. I say give the solution to openCage. I can elaborate a little further on his answer with some links. If you want the variable to be passed by reference you can wrap it in a class(How do I pass a primitive data type by reference?), but I would recommend the idea of simply returning the boolean value that rgettman mentions above.

Here is a link to the documentation on Java Primitives which are not implemented as classes.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Community
  • 1
  • 1
Jesse Ivy
  • 335
  • 1
  • 10
1

Because parameters in Java are passed by value

 int x = 1;
 fn(x);
 System.out.println(x); // nothing changed

 public void fn(int x){
     x++; // changes stay local to fn
 }

A common way to solve this is to return the value you changing

public int fn(int x){
   x++;
   return x;
}

and now you can assign the new value to x

int x = 1;
x = fn(x);
System.out.println(x); // it works now, Brilliant!
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

In the removeAccess method you are setting local variable eq to true. It's the variable that was declared as a parameter that is being set.

You would want to declare eq outside of a method and make it a class member and just manipulate it inside of your methods. For the removeAccess method you can still accept it as a parameter but use this.eq = eq; to assign the argument value to the class variable. Once you have assigned the value, continue to use this.eq(class) to differentiate between the local eq and class eq.

MarGar
  • 465
  • 5
  • 12