• Post Reply Bookmark Topic Watch Topic
  • New Topic

Output of this program  RSS feed

 
Aman Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellows,

I got few java problems on some site. This question is really bugging me.
As per the policy I am not posting the answer here, so that other people can learn too... but I am really confused over its solution. Why its coming that way.

 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your question? If you have the solution and if you're not sure why is it that way, then please post the solution and tell us which part you couldn't understand.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aman Singh wrote:I got few java problems on some site.

Please note that when you copy a question from a book, mock exam or other source, it is required on JavaRanch that you QuoteYourSources - in other words, tell us where you copied this question from. So, please tell us which website you got this from.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and when we say "don't post the answer", what we mean is when someone says "I have to write a program that does A,B and C", don't post the code that would do A, B and C.

as to the program, you have to realize that each Mixer object has a reference to another Mixer object - the m1 in each and every Mixer object points to some other one...unless it points to 'null'.

on line 12, you make a Mixer reference called m2 and a new mixer object, who's m1 points to null, since no mixer object was passed in to that constructor.

on line 13, you make a Mixer reference called m3. You make a new Mixer object, passing in m2, so the new object's m1 points to m2.

you call m3's go() method, printing out "hi".

you make Mixer reference m4, which really is just a new additional reference to m3.

you call m4's go() method, printing out "hi".

you then make a new Mixer reference called m5, and point it to the object pointed to by m2's m1 reference...which is still 'null'.

so m5 is NOT pointing to any object. so when you call m5.go(), you get a NPE.
 
Anupama Sudhakaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can any1 tell me what "m3.m1" means when m3 and m1 both are objects of the same class
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"m3" is a variable of type Mixer. Each mixer has a field of type Mixer called "m1". "m3.m1" is the "m1" field of variable "m3".
 
Anupama Sudhakaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dint know it was possible to have something like m3.m1
Thank you
 
Anupama Sudhakaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is there some special term for something like m3.m1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!