File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question" Watch "Question" New topic


Sherman Hung

Joined: Dec 15, 2001
Posts: 17
class ValHold{
public int i = 10;
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
public void amethod(){
int i = 99;
ValHold v = new ValHold();
System.out.print( v.i );
}//End of amethod
public void another(ValHold v, int i){
v.i = 20;
ValHold vh = new ValHold();
v = vh;
}//End of another
1) 10030
2) 20030
3) 209930
4) 10020
What is the correct answer?
I can understand 10 and 0 but 20. Is Java call by reference or call by value? Why it is not 30?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
All java calls are by value, i.e value is copied and passed to formal parameter of method.
In another() method when you change the value
v.i = 20;
it changes the value where v points and at that time it was pointing to ValHold object created in amethod().
hope this example will remove ur all doubt.

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
I agree. Here's the link:
subject: Question
It's not a secret anymore!