| Author |
Explanation needed
|
vinodh kumar
Ranch Hand
Joined: Jan 07, 2008
Posts: 41
|
|
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); } }
|
VINODH KUMAR
/**SCJP 1.5***chemical engineering rocks*/ [My Wiki]
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
|
dog[2] refers to the same Dog object as dog[1].
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
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].
|
Joanne
|
 |
vinodh kumar
Ranch Hand
Joined: Jan 07, 2008
Posts: 41
|
|
|
Thanks a lot.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
|
You may have to go through this link for Pass by value, please!.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
 |
|
|
subject: Explanation needed
|
|
|