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);


}
4
  • 1
    because parameters are passed by value in Java Commented Nov 7, 2014 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. Commented Nov 7, 2014 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 Commented Nov 7, 2014 at 19:47
  • @Rika I will post it as an answer then Commented Nov 7, 2014 at 19:51

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

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

Comments

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!

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.