This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explanation needed

 
vinodh kumar
Ranch Hand
Posts: 41
  • 0
  • 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
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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
Pie
Posts: 3742
16
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to go through this link for Pass by value, please!.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic