Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do i get a null?  RSS feed

 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks guys it was really helpful.
 
Oceana Wickramasinghe
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!