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;
}
}
Use obj1.add200really(obj1);
Because you have no method add200really(int)
Your method add200really()
requires an object. You're trying to call it with an int
精彩评论