aspose file tools*
The moose likes Beginning Java and the fly likes pass by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pass by value" Watch "pass by value" New topic
Author

pass by value

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

i getting the out put as 200. as we knoe java is passby value but im getting 200 as out please help me


Creativity is nothing but Breaking Rules
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

This theory doesn't prevent you from modifying an object's state. Objects you pass to a method are not immutable. The reference to the object will not change, but it's content can be changed.
Check what happens with the following :


[My Blog]
All roads lead to JavaRanch
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

The reference to the object will not change, but it's content can be changed.

as you told reference will not change means inside the method. method reference and actual reference are pointing to the same object wright and after the method scope the method reference will be no more valid is it correct.l
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

santhosh.R gowda wrote:after the method scope the method reference will be no more valid is it correct.l


Yes this is correct. However the original reference will still be valid - you can still use it.

If there still confusion I'll recommend you get your hands on Head First Java. The got a really nice 'remote control' which should makes things crystal clear & a whole lot of other concepts too (Of course!)


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
priya rishi
Ranch Hand

Joined: Oct 26, 2008
Posts: 119
points to note:

1.class test
2.only one instance of class test is created.
test p = new test(100);

3.p is the reference variable which holds the address of object test, with i = 100.
4.in getRef(p) - you are passing the address of object test, as p holds the address.
5. public static void getRef(test obj)
{
obj.i=200;
}

inside this method ,
no new object is created.

obj has the same address as p.so obj and p refers the same object of class test, where the i's value is 100.

6. when you assign the value for i using this statement,
obj.i=200;

you are manipulating the one and only object, by changing its i value to 200.

7.that's how you get 200 as output,using this print statement.
System.out.println(p.i);





[Thumbnail for test.jpg]


SCJP 5 , SCWCD 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

See the following JavaRanch Campfire Story to understand what pass by value means exactly in Java:
Pass-by-Value Please


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by value