• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null instance variable after assigning a value

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why this prints null ?
[ October 02, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should test2.s be set to "hello"? You set test1 to "hello" -- not test2.

Henry
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, but when i run the code the two instances executes, and the first one assigns "Hello" to s ...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, but what about this ?



it prints "8"
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic