user133466
user133466

Reputation: 3415

Java pass by reference vs pass by value trouble

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

Upvotes: 0

Views: 128

Answers (2)

Brian Roach
Brian Roach

Reputation: 76888

Because you have no method add200really(int)

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

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94625

Use obj1.add200really(obj1);

Upvotes: 1

Related Questions