Here is a question from Marcus public class StrEq{ public static void main(String argv[]){ StrEq s = new StrEq(); } private StrEq(){ String s = "Marcus"; String s2 = new String("Marcus"); if(s == s2){ System.out.println("we have a match"); }else{ System.out.println("Not equal"); } } }
The correct answer is "Not Equal".But according to RHE,the private constructor cannot be instantiated.Here,dont u think that it gives compiler error since the private constructor is instantiated?Please help me. Thanks rubna
Paul Smiley
Ranch Hand
Joined: Jun 02, 2000
Posts: 244
posted
0
A private constructor can be called from within its own class, as it is in this instance.
Marcus Green
arch rival
Rancher
Joined: Sep 14, 1999
Posts: 2813
posted
0
The reason I try to include entire classes in questions is so you can compile and try it yourself to see what happens. It is a great way to learn. Marcus
Originally posted by Marcus Green: The reason I try to include entire classes in questions is so you can compile and try it yourself to see what happens. It is a great way to learn. Marcus
Thanks, I tried compiling,but i did'nt get any error.That's why i was confused.I thought that it would give a compiler error telling private constructors cannot be instantiated. Thanks,i got it.