wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can anyone explain me what is happening at line 8? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can anyone explain me what is happening at line 8?" Watch "Can anyone explain me what is happening at line 8?" New topic
Author

Can anyone explain me what is happening at line 8?

Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
1.class Mixer {
2.Mixer() { }
3.Mixer(Mixer m) { m1 = m; }
4.Mixer m1;
5.public static void main(String[] args) {
6.Mixer m2 = new Mixer();
7.Mixer m3 = new Mixer(m2); m3.go();
8.Mixer m4 = m3.m1; m4.go();
9.Mixer m5 = m2.m1; m5.go();
}
void go() { System.out.print("hi "); }
}
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

At line 8, you are calling no arg constructor that you have defined at line 2.
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

also for this reason line number 9, will give you NullPointerException as m2 has no object which you are asking it to return.
Tushar Gosalia
Greenhorn

Joined: Feb 19, 2013
Posts: 27
No i'm asking about
Mixer m4 = m3.m1; m4.go();
What does this statement mean??And What will be stored in m4??
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

m4 will refer to m2 object, as you kept it in m3 and asking it to return that object.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
Object reference diagram:
m2 -> o where o is an object
m2.m1 -> null
m3 -> o1 where o1 is another object
m3.m1 -> m2 ->o
m4 -> m3.m1-> m2 -> o
m5->m2.m1-> null
So, m5.go() returns a null pointer exception
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anyone explain me what is happening at line 8?
 
Similar Threads
explain output
Please Explain
K & B question ???
Exception
explain me this code