Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java eclipse String Error

I'm New to Java and I am following some instruction, However When I get to the Strings section

public class String {
    public static void main(String[] args) {
        java.lang.String name;
        name = "luke";
        System.out.println("Hello, " + name + "pleased to meet you");
    }
} 

But I Get

Error: Main method not found in class String, please define the main method as:
    public static void main(String[] args)
like image 908
user2329649 Avatar asked Nov 30 '22 21:11

user2329649


2 Answers

If you insist on using String as your class name it should be:

public class String {
    public static void main(java.lang.String[] args) {
        java.lang.String name;
        name = "luke";
        System.out.println("Hello, " + name + "pleased to meet you");
    }
} 

I don't think it's particularly wise to try and re-use the names of classes defined in java.lang though.

like image 68
Philip Whitehouse Avatar answered Dec 04 '22 06:12

Philip Whitehouse


Since your class is named String, it is being inferred by the compiler as the argument type of your main method.

Try fully qualifying the argument type instead:

public static void main(java.lang.String[] args) {
...

Or better yet, rename your class to use and non-java.lang class name.

like image 20
Brent Worden Avatar answered Dec 04 '22 07:12

Brent Worden