• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Why do i get a null?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain why does it print null? Im sure it has something to do with the constructors but i cant figure it out. Thanks

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class Target has two member variables; a String s (line 9) and a Target o (line 13). It also has two constructors: one that sets member variable s and leaves o set to null (lines 10-12), and one that sets member variable o and leaves s set to null (lines 14-16).

In line 4, you are creating a new Target object with the second constructor. In the Target object that's created, o refers to the already existing Target object (which was created in line 3) and s is set to null.

Then you create and start a thread with this Target object, which prints the content object, for which Target.toString() is called. The toString() method just returns s. Because s is null in the Target object that was created in line 4, you see "null" printed.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you QuoteYourSources please?

System.out.println(this) will end up printing the result of this.toString(). This returns the value of s. Now, have you initialised the value of s in the object that will be called on? Remember there are two Target objects created, and only one of them is given an s value.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys it was really helpful.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Can you QuoteYourSources please?



I actually ran into this myself while writing a program.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i guess this means that changes made within a constructor that is associated with a particular instance, does not apply to other instances?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oceana Wickramasinghe wrote:I actually ran into this myself while writing a program.


Fair enough. Sorry! It looked a bit like an SCJP question.

Oceana Wickramasinghe wrote:So i guess this means that changes made within a constructor that is associated with a particular instance, does not apply to other instances?



That's right. The job of a constructor is to initialise a particular instance.
 
Marshal
Posts: 76392
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend that you ensure that every constructor initialises every field to a “consistent” value, i.e. one which maintains the class invariants. That design with a self‑referential class makes that particularly difficult.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic