0

why am i getting an error in the pass by reference example obj1.add200really is underlined

public class Test {

    private int number;

    Test(){
        number = 1;
    }

    public static void main(String[] args) {
        Test obj1 = new Test();
        System.out.println("the number is " + obj1.number);
        System.out.println("the number 1 plus 200 is " + obj1.add200(obj1.number));
        System.out.println("while the number is still " + obj1.number);
        System.out.println("\n");
        System.out.println("the number is " + obj1.number);
        System.out.println("the number 1 plus 200 is " + obj1.add200really(obj1.number));
        System.out.println("while the number is still " + obj1.number);
    }


int add200(int somenumber){
    somenumber = somenumber + 200;
    return somenumber;
}
int add200really(Test myobj){
    myobj.number = 999;
    return myobj.number;
}
}

2 Answers 2

1

Use obj1.add200really(obj1);

Sign up to request clarification or add additional context in comments.

2 Comments

@user133466 - I don't think so because you do not know the basic facts - Java is always pass-by-value. Please read this thread - stackoverflow.com/questions/40480/is-java-pass-by-reference
someObj.func(someObj) is almost never the right thing to do. @user133466 I don't think you have understood the problem, leave alone the solution.
0

Because you have no method add200really(int)

Your method add200really() requires an object. You're trying to call it with an int

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.