Your code is not valid Java code. It does not compile. I fixed the compilation errors using the following steps:
- changed the return type of the method
main
to void
- removed
throws IOException
, since no exception is thrown the compiler complains about it
- closed the method
main
before opening the method createMessage
- made the method
createMessage
static, so it can be called from the static method main
- added
String
as return type to the method createMessage
- moved return statement to the method
createMessage
This is the fixed code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(createMessage());
}
public static String createMessage() {
String message = "Hello World!";
return message;
}
}
You probably do not want to call the main method by yourself from another method. The main method is the program's entry point which means that it is automatically called by the JVM to start your program. In the main method, you print the hello world message. You do this by creating the message with the method createMessage
. After this method finished execution, you pass the create message to the method System.out.println()
which is Java's method to output text to the console.
You can further simplify your code by replacing the two lines
String message = "Hello World!";
return message;
with this line:
return "Hello World!";