public static void main(String[] args) {
Becuase you must use a java.lang.String
, not your own. In your main method, the String
you're using is actually the custom String
that was defined, not a real java.lang.String
.
Here is the code, clarified a bit:
class MyString {
private final String s;
public MyString(String s){
this.s = s;
}
public String toString(){
return s;
}
public static void main(MyString[] args) { // <--------- oh no!
MyString s = new MyString("Hello world");
System.out.println(s);
}
}
So, the lesson that you can learn from this puzzle is: don't name your classes as other commonly used classes!