| Author |
Please Explain
|
Thirumalai Muthu
Ranch Hand
Joined: Oct 07, 2007
Posts: 75
|
|
class Mixer { Mixer(){} Mixer(Mixer m) {m1=m;} Mixer m1; public static void main(String [] args) { Mixer m2=new Mixer(); Mixer m3=new Mixer(m2); m3.go(); Mixer m4= m3.m1;//line1 m4.go(); Mixer m5= m2.m1; m5.go();//line4 } void go() { System.out.print("hi"); } } Can anyone Please explain the lines from 1 to 4.Thanks
|
SCJP 5
|
 |
Robert Elbourn
Ranch Hand
Joined: Oct 15, 2007
Posts: 69
|
|
class Mixer { Mixer(){} Mixer(Mixer m) {m1=m;} Mixer m1; public static void main(String [] args) { Mixer m2=new Mixer(); Mixer m3=new Mixer(m2); m3.go(); Mixer m4= m3.m1;//line1 - m4 is created using m1 as a parameter (not null) m4.go(); - this is invoked Mixer m5= m2.m1; - m5 is created using m2.m1 which is null!!! as m2 was created using a direct call without passing a parameter as m2.m1 returns a null value then m5 is null m5.go();//line4 - any method on a null returns my favourite exception (nullpointer exception) } void go() { System.out.print("hi"); } }
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Please Explain
|
|
|