aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes proble with wraper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "proble with wraper class" Watch "proble with wraper class" New topic
Author

proble with wraper class

kavan desai
Ranch Hand

Joined: Jan 16, 2005
Posts: 32
Friends public class wrappertest{
void overWrite(Double d)
{
d=null;
d=new Double(7.0);

}
public static void main(String args[])
{
Double d=new Double (5.0);
wrappertest t=new wrappertest();
t.overWrite(d);
System.out.println(" "+d);

}
}
please solve my problem it is very basic but ... i have a dought the above given code should print 7.0 as object is passed by referance and we r assigning d to new refernce they why it showing me out put 5.0 instade of 7.0 can i get the out put like 7.0 please explain me how?
thanks
Kavan
Preaparing for Scjp 1.4.0
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Hi,

First, a bit of business: the javaranch Naming Policy requires that you use a real first and last name.

You could head over here to fix that.

Second, this is a great question for the
Programmer Certification (SCJP) forum. Maybe a kind bartender or sheriff will move this post over there.

Third, regarding your question... check out this info on how java passes by value

Javaranch FAQ on call by reference vs. value

pass by value article
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In a nutshell, the main method instantiates object d, and will continue to hold a reference to it, no matter what is done in overWrite(). Even though overWrite() abandons it's reference to the Double object with a value of 5.0 and creates a new one with a value of 7.0, main() still holds the original reference with a value of 5.0.

This type of manipulation only works with mutable (changeable) objects. Since Double is immutable, the only way you can change it is to drop the reference and create a new one.

If the object passed as a parameter were a mutable object, such as a StringBuffer, overWrite could manipulate the object, and main() could see the results of that manipulation. However, it is impossible for a called method to make the calling method give up or replace it's reference to an object.


Merrill
Consultant, Sima Solutions
kavan desai
Ranch Hand

Joined: Jan 16, 2005
Posts: 32
Thanks Carol & Merrill for replying me actully i am bit new to this forum.
Thanks.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
We're glad to have you stick around. Please change your name to match our standards, though, as that helps us to maintain a friendly atmosphere. Also, choose the forum carefully to increase your chances of getting a good and quick answer. The SCJP or the Java in General (Beginner) forums would have been perfect.
 
 
subject: proble with wraper class