Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus exam Q30

 
rubna
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A private constructor can be called from within its own class, as it is in this instance.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rubna
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic