| Author |
null instance variable after assigning a value
|
Juan Manuel Alberto de los Santos
Ranch Hand
Joined: Jun 26, 2008
Posts: 48
|
|
why this prints null ? [ October 02, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
It wont compile since nc1 is undefined.... BTW....A member variable of a class that is a reference variable (anything that extends Object) will always be initialised to null unless otherwise specified. tom
|
<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
|
 |
Juan Manuel Alberto de los Santos
Ranch Hand
Joined: Jun 26, 2008
Posts: 48
|
|
sorry my mistake, obviously i didn't want to ask that ... the question still remains, why "s" value is "null" instead of "hello" ... thanks [ October 02, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Why should test2.s be set to "hello"? You set test1 to "hello" -- not test2. Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Juan Manuel Alberto de los Santos
Ranch Hand
Joined: Jun 26, 2008
Posts: 48
|
|
|
yes, but when i run the code the two instances executes, and the first one assigns "Hello" to s ...
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9952
|
|
each instance of the test class has it's own 's' variable. just like every car has it's own odometer reading. if you add the line System.out.println(test1.s); you'll get your "Hello" printed out.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Juan Manuel Alberto de los Santos
Ranch Hand
Joined: Jun 26, 2008
Posts: 48
|
|
ok, but what about this ? it prints "8"
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
It prints 8 because f2 goes to f1 and changes the x value to 8. Now can you explain why f2.x is *not* 8? Henry
|
 |
Ravikanth kolli
Ranch Hand
Joined: Feb 10, 2008
Posts: 179
|
|
hye juan, for all the instances that you create of a class, will have its own set of instance variables that you define. In the first example you set the String in test1 to "Hello" but not String of test2. so you will be getting null/ In the second example, you are passing the f1 as a parameter to the constructor of f2. Where you are actually setting the value of f1's x to 8. Hence you get an 8
|
-kolli
|
 |
Juan Manuel Alberto de los Santos
Ranch Hand
Joined: Jun 26, 2008
Posts: 48
|
|
the constructor uses f1 object reference and changes the x value to 8 in f2 the reference is simply pointed to the original x, that's why is 5 ... i don't know if it's correct, is kind of hard to understand
|
 |
Ravikanth kolli
Ranch Hand
Joined: Feb 10, 2008
Posts: 179
|
|
We are not considering the value of f2's x here in this case. The new Fizz object that you create in the constructor is actually a reference to the original f1 object. So the initial value for f1's x is 5 and now you are changing the value to 8 with in the constructor of f2.
|
 |
Stephen Davies
Ranch Hand
Joined: Jul 23, 2008
Posts: 352
|
|
Its pretty straight forward. You have decalred a no-argument constructor and an overloaded constructor which takes a String as an argument. Thus whenever you call new on an instance of the class, the constructor will either run with the no-argument constructor which initializes nothing, or it takes a string which returns initializes the String variable to the value "Hello". Since you are calling your print function on test2.s, test2 is a new instance of test in which you have not pased a String variable, and thus the instance will initialize the test2 instance using the no-argument constructor. Therefore s has not been initialized by you with any value and thus gets a null from the JVM. If you had done the same with an int you would get 0, a boolean, false and so on.  [ October 03, 2008: Message edited by: Stephen Davies ]
|
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
|
 |
 |
|
|
subject: null instance variable after assigning a value
|
|
|