This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Explanation needed  RSS feed

 
vinodh kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one explain me why the ouput is Goofy,it should be loofy na.


public class Dog {
String name;

public static void main(String[] args) {

Dog[] dog = new Dog[3];
dog[0]=new Dog();
dog[1]=new Dog();
dog[2]=dog[1];

dog[1].name="loofy";
dog[2].name="goofy";

System.out.println(dog[1].name);

}
}
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15861
81
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dog[2] refers to the same Dog object as dog[1].
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line
dog[2]=dog[1];
means dog[1] and dog[2] both refer to the same object, therefore any change you make to the object referenced by dog[2] will also be seen in the object referenced by dog[1].
 
vinodh kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to go through this link for Pass by value, please!.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!