File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Argument passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Argument passing" Watch "Argument passing" New topic
Author

Argument passing

Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Hi. I', preparing for the programmers exam. I have many questions but i'll start with argument passing.
I'm reading complete Java 2 certification by Simon Roberts , Philip Heller and micheal Ernest. Although I've read the book I don't get the argument passing. Sometime the value changes and sometimes it doesn't?
Example:
Class Q6{
public static void main( String args[] ){
Holder h = new Holder();
h.held();
h.bump();
System.out.println( h.held );
}
}
Class Holder{
public int held;
public void bump( Holder theHolder ){
theHolder.held++
}
}
Question. Why is the output 101 and not 100?

------------------
Preparnig for the Java 2 Certification exam


Preparing for the Java 2 Certification exam
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
I have no idea why the output would be 100 or 101. Your example won't compile. You have a call to the method held() with the statement: h.held();
Yet there is no method held defined in this example, only a variable. Even if we removed that statement, there would be no way 100 or 101 would be the output. You had better check this one.
- Sean
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Sorry about this I wrote it wrong... here we go again.
Note. I've search for argument passing but I don't understand it anyway.
Example again.

Class Q6{
public static void main( String args[] ){
Holder h = new Holder();
h.held = 100;
h.bump( h );
System.out.println( h.held );
}
}
Class Holder{
public int held;
public void bump( Holder theHolder ){
theHolder.held++;
}
}
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Ok, now we're on the same page. You've probably already read this but I'll say it and then go over it with the example. Objects are pass by reference while primitives are pass by value, and furthermore, a copy of the reference or a copy of the value is passed onto the method. Since the bump method takes a copy of the reference(h), the method is able to manipulate the object that h references, and in this case it adds one to the held variable. Now if you had a method declared as follows:
int h= 100
public void bump(int h){
h++
}
System.out.println(h);
In this instance h would =100. The bump method takes a copy of the primitive value of h (100) and then increments it as one. Since this is only a copy of the value, it does nothing to change the original value. The key to understand is that a reference to an object can change an object, even if that reference is a copy. This is fundamental, so take your time with it. It is very important to understand. So to reiterate I'll go through your example:
in line 3 a new Holder is created referenced by the variable h.
in line 4 a h's instance variable held is intialized to 100.
in line 5 a call to the method bump with a copy of the reference h sent to it. Remember h refers to the original Holder object, so whatever changes occur in the bump method directly effects the object that h references. In this case the h's instance variable is incremented by one, hence why you get 101 instead of 100.
I know I might not explained that as clearly as I would like, so please ask more questions if you have some. When I studied this the first time, I kind of skimmed right over it, and I had to go back later on and really study it hard. I urge you not to move on until you absolutely understand this concept. You'll see stuff like this on the exam. Trust me.

- Sean
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Thanks Sean
I will read this carefully. And do examples on it. Yes I know that this is very important to know so I will do as many exaples on this as possible. Please come back to this topic later and I will have more questions.
Thanks for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Argument passing