user2329649
user2329649

Reputation: 75

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)

Upvotes: 4

Views: 3828

Answers (5)

roridedi
roridedi

Reputation: 11

why don't you try this

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

}

Upvotes: 0

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

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.

Upvotes: 7

jlordo
jlordo

Reputation: 37853

As your class hides the java.lang.String name, you need to write

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

Better call your class StringTest or something else to avoid this confusion.

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

Upvotes: 2

rgettman
rgettman

Reputation: 178363

You were careful to fully-qualify your reference to java.lang.String for the name variable, but not for the args parameter to main.

Use

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

Of course, this all resulted because you named your class String, the same name as a built-in class in Java. Perhaps you could name it StringTest instead? That would avoid having to worry about class name collision and fully qualifying the Java built-in String.

Upvotes: 5

Brent Worden
Brent Worden

Reputation: 10994

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.

Upvotes: 5

Related Questions