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,136
  • 12
  • 51
  • 73
user133466
  • 3,391
  • 18
  • 63
  • 92

2 Answers2

1

Use obj1.add200really(obj1);

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 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
  • 76,169
  • 12
  • 136
  • 161