Java Reference
In-Depth Information
Solution 67: All Strung Out
This program looks simple enough, if a bit repulsive. The class String in the unnamed package is
simply a wrapper for a java.lang.String instance. It seems the program should print Hello
world . If you tried to run the program, though, you found that you could not. The VM emits an error
message something like this:
Exception in thread "main" java.lang.NoSuchMethodError: main
But surely there is a main method: It's right there in black and white. Why can't the VM find it?
The VM can't find the main method because it isn't there. Although StrungOut has a method named
main , it has the wrong signature. A main method must accept a single argument that is an array of
strings [JVMS 5.2]. What the VM is struggling to tell us is that StrungOut.main accepts an array of
our String class, which has nothing whatsoever to do with java.lang.String .
If you really must write your own string class, for heaven's sake, don't call it String . Avoid reusing
the names of platform classes, and never reuse class names from java.lang , because these
names are automatically imported everywhere. Programmers are used to seeing these names in their
unqualified form and naturally assume that these names refer to the familiar classes from
java.lang . If you reuse one of these names, the unqualified name will refer to the new definition
any time it is used inside its own package.
To fix the program, simply pick a reasonable name for the nonstandard string class. The following
version of the program is clearly correct and much easier to understand than the original. It prints
Hello world just as you'd expect:
public class StrungOut {
public static void main(String[] args) {
 
 
Search WWH ::




Custom Search