简体   繁体   中英

In Java why cant we use an inner class with name as String if the parent class has psvm

public class OuterClass {
    public static void main(String[] args) {
        System.out.println("Hello !");
    }

    private class String {
      int i = 10;
    }
}

The above code compiles fine

When I tried to run the above code, it is throwing error as " Error: Main method not found in class , please define the main method as: public static void main(String[] args)" . Any reason why such a Runtime Exeption occurs ?

Because your inner class has higher visibility than java.lang.String ; thus you have changed the main signature. Change your main like

public static void main(java.lang.String[] args) {
    System.out.println("Hello !");
}

Or rename your class String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM