I have the below code which throws the error given below. when the 'String[] args' is replaced with 'java.lang.String[] args' it doesn't throw error and runs method s1.method1().
Problem: why is the local String given preference over java.lang.String ? according delegation model the Bootstrap class loader gets preference and java.lang.String loaded before the Test class is loaded. Am I missing something here. please point me to any reference for this behavior.
Test.java:
package diff;
class String {
public void method1(){
System.out.println("in my method");
}
}
class Test {
public static void main(String[] args){
String s1 = new String();
s1.method1();
}
}
Error: Main method not found in class diff.Test, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Problem: why is the local String given preference over java.lang.String ?
The local name masks the one in java.lang
. The java.lang
package is implicitly imported by the compiler for every program, however you can't use java.lang.String
as String
because you masked the name String
. As you noted, you can use java.lang.String
or rename your String
class (in short, don't add custom classes that conflict with class names from java.lang
). This is not a class-loader issue, as after compilation all classes are fully qualified in the byte code.