+ 2

Why this program is generating the exception?

Why this program is generating the exception?? public class R { String s = "RA!"; R A; R(){ System.out.print(A.s); } } class test { public static void main(String [] g) { R B = new R(); } }

1st Jan 2018, 3:12 PM
Rishabh
Rishabh - avatar
2 Respuestas
+ 1
Because the constructor for A is currently running and A hasn't been assigned as yet so it is null.
1st Jan 2018, 3:21 PM
John Wells
John Wells - avatar
+ 1
Forget my previous post. If that were true, I should have gotten two outputs not one. It is because A is uninitialized as you need to assign this to it.
1st Jan 2018, 3:31 PM
John Wells
John Wells - avatar