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;
}
}
subodh
  • 6,004
  • 12
  • 48
  • 69
user133466
  • 3,301
  • 16
  • 58
  • 90

2 Answers2

1

Use obj1.add200really(obj1);

KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
  • 4
    @user133466 - I don't think so because you do not know the basic facts - **Java is always pass-by-value**. Please read this thread - http://stackoverflow.com/questions/40480/is-java-pass-by-reference – KV Prajapati Sep 22 '11 at 04:25
  • `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. – Miserable Variable Sep 22 '11 at 04:34
0

Because you have no method add200really(int)

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

Brian Roach
  • 74,513
  • 12
  • 132
  • 160