• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query regarding wrapper classes  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
rahul lahiri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually my question was why the value of no is not getting changed to 100 like the x variable of the mv object.
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cal lBy Reference Vs Call By Value
 
rahul lahiri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What specifically is giving you a problem?
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you even read the page I linked to, and the pages that page links to? I very much doubt it.
 
rahul lahiri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologize :)
 
Rob Spoor
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeedy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!