This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Query regarding wrapper classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Query regarding wrapper classes" Watch "Query regarding wrapper classes" New topic
Author

Query regarding wrapper classes

rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
I am new to java.I was trying with the following code:-








The output that I am getting is
200
5
Now last day, I learned that Wrapper classes are objects.So no is an object but its not behaving like one.I am new to OOP and java and I don't have a clue whats happening.Please help.

Thanks in advance
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
Please make habit of putting code in code tags.


I dont find any complicated stuff here. You are passing mv object to function valueChanger and setting its x variable value to 200. If you are confused between int and Interger go ahead read about Boxing (auto-Boxing)


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
Actually my question was why the value of no is not getting changed to 100 like the x variable of the mv object.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It *is* being changed--inside the method that's changing it. But you're doing different things: one statement sets the reference to a new value. One sets a member of a reference to a value. Try setting "mv" itself to a new value--what happens in the caller?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Cal lBy Reference Vs Call By Value


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
@David
Yeah.It doesn't reflect in the main class then.But it is getting confusing.What is the rule then.If I pass an object and change the value of its member, then it would reflect outside, in the caller class and if I pass an object and change what it refers to,then it won't reflect?Is this the rule?Thank you very much for your reply.
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
Hi Rahul, I dint understand your question properly at the beginning. Have a look at following.
in the caller class and if I pass an object and change what it refers to,then it won't reflect?Is this the rule?

Yes.
Try modifying your class as follows

And modify method like follows

In method valueChanger, you are changing content of object MyValue and you are reassigning 'no' to 100. Since you are reassigning 'no', change dint appear in calling method.
Now see in method valueChanger, I am doing mv = new MyValue(30,40); which is not at all affecting output.
rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
Thank you for your reply.I did exactly this and got this output after getting the reply from David.The change in reference of the object does not reflect in the caller method.Now,like I posted before, what is the rule in java in such situations?If I change the object reference inside a method and make it refer to another(of course a compatible one), it won't reflect outside the method in which I am doing it but if I change the value of its members,then it would reflect?What if a member is an object itself?And if this is the rule, then why?I thought objects were passed by reference in java. I am really confused.Please help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What specifically is giving you a problem?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Have you even read the page I linked to, and the pages that page links to? I very much doubt it.
rahul lahiri
Greenhorn

Joined: Mar 20, 2009
Posts: 13
Yep.I didn't notice it.Yeah, it is clear now.I should have read that and then asked rest of the questions.I am very very sorry and thank you for clarifying things.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No need to apologize :)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Indeedy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query regarding wrapper classes
 
Similar Threads
a program on wrapper classes
overloading
Arrays
question about argument passing
Wrapper class ??