This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passing Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Arguments" Watch "Passing Arguments" New topic
Author

Passing Arguments

Chatura Dilan
Greenhorn

Joined: Apr 22, 2006
Posts: 16
public class T5{

public static void main(String args[]){

T6 t6_1 = new T6();
T6 t6_2 = new T6();
T6 t6_3 = new T6();

t6_1.test1(t6_2);
t6_1.test2(t6_3.x);

System.out.println(t6_2.x);// prints 20
System.out.println(t6_3.x);// prints 10
}
}


class T6{

int x = 10;

public void test1(T6 tt){

tt.x = 20;
}

public void test2(int y){
y = 30;
}


}


I wonder why the second System.out.println statement does not print 30 like the first System.out.println does.
Can anyone explain it why?

[ April 25, 2006: Message edited by: Chatura Dilan ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Java passes everything by value. In the first example you modify the actual instance member of the T6 object, in the second example you simply modify a copy of the variable that was passed.
Chatura Dilan
Greenhorn

Joined: Apr 22, 2006
Posts: 16
Why doesn�t it pass the copy of the instance member of the T6 object, like the second example pass the copy of the variable?
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Reader,
I would suggest you to read this article from JavaRanch Campfire Story : Pass By Value Please. Its an excellent piece of article.


With Best Regards,
Shyam Prasad Murarka
Chatura Dilan
Greenhorn

Joined: Apr 22, 2006
Posts: 16
What a nice artical

Thank you Shyam
and Ken too
 
Don't get me started about those stupid light bulbs.
 
subject: Passing Arguments
 
Similar Threads
a simple question
LOCAL VARIABLE
LOCAL VARIABLE
Please help me
LOCAL VARIABLE