Skip to main content

Hi guys i need help on this.

1 reply [Last post]
shueishalee
Offline
Joined: 2012-12-13
Points: 0

I am new to java.Can you please help me.

I am using eclipse and I am getting an error in the below code.

Please reply me with correct solution for this.

public class SimpleApplication {
public static void main(String[] args) {
Welcomer welcomer = new Welcomer();
welcomer.SayHello();
}
}

public class Welcomer {
private String welcome = "Hello!";
public void SayHello(){
System.out.println(welcome);
}
}

And also tell me where is the error has occurred.when I compile I get this error
"The public type Welcomer must be defined in its own file"

Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rwlarsen
Offline
Joined: 2008-03-28
Points: 0

you are limited in java to one public class per file. The filename must match the classname. This allows the jvm to easily locate classes based on the filenames only.

You can:
create another class file for the Welcomer class
or
make the Welcomer class private (only accessible by SimpleApplication).