Unclear on Object references.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Rooks
Posted On:   Tuesday, January 29, 2002 01:15 PM

I don't understand. public class Party{   String name; } public class Shipper extends Party{   String point; } public Party getParty{   Party p = new Party();   p.name = "A Name";   return p; } public void setParty( Party p ) {   p.name = "A Name"; } Why doesn't this change the Shipper's name?   Shipper s = new Shipper();   Party p = s;   p = getParty(); When this does.   setParty( s );    More>>
I don't understand.


public class Party{

  String name;

}


public class Shipper extends Party{

  String point;

}


public Party getParty{

  Party p = new Party();

  p.name = "A Name";

  return p;

}


public void setParty( Party p ) {

  p.name = "A Name";

}



Why doesn't this change the Shipper's name?


  Shipper s = new Shipper();

  Party p = s;

  p = getParty();


When this does.


  setParty( s );

   <<Less

Re: Unclear on Object references.

Posted By:   Steve_Long  
Posted On:   Tuesday, January 29, 2002 06:28 PM

in the getParty method, a new instance of party is created and then the name attribute of the new instance is assigned. in the setParty method, a new instance is not created. if the line Party p = new Party(); is removed from the getParty() method, the name attribute will be set as it is in the setParty() method.
About | Sitemap | Contact