Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass by value

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i getting the out put as 200. as we knoe java is passby value but im getting 200 as out please help me
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!)
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);



test.jpg
[Thumbnail for test.jpg]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15641
47
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the following JavaRanch Campfire Story to understand what pass by value means exactly in Java:
Pass-by-Value Please
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic