0

If I have a class:

public class HelloWorld {
  public static String main(String[] args) throws IOException {

 public createMessage(){

 String message = "Hello World!";

  }

 }
 return message; //return of the main static method
}

Why can't I call main as HelloWorld.main(args) from another class?

String msg = (String) HelloWorld.main(args);

System.out.println(msg);

As per the way we call static methods.

ninjayoto
  • 693
  • 1
  • 7
  • 14
  • 1
    To call a method you need to write code that **compiles**. – fabian Feb 25 '16 at 00:29
  • There is nothing wrong with using the main method to do some work. It is typically used in two situations (for me at least): laziness or to test something you wrote. Usually when I have a Java program that calls another Java program, I run the class or Jar as a new process so that it will not share memory space, states (OS level), or global application variables. If you call the main method from the current JVM, it will either use the current thread or a new thread if you choose. But please don't do this within the same JVM unless you are just doing it out of laziness. Stefan's method is best. – ldmtwo Feb 25 '16 at 06:23

2 Answers2

2

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!";
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
  • And to call it from another class you instantiate an object of HelloWorld? sometimes when I try to add a method under main, the IDE just don't recognize it at all , Eclipse, it keep telling : Syntax error, insert ";" to complete LocalVariableDeclarationStatement – ninjayoto Feb 25 '16 at 01:22
  • (1) You cannot insert a method declaration into another method. Here, `main` is also method, so you cannot insert a method declaration into the `main` method. (2) `HelloWorld` has only [static elements](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class). This means there is no point to create an instance of it. To call the `main` method from another class, you would do it just as in your question: `HelloWorld.main(new String[0]);`. It does not return a value, but only print it to the console. Are you sure you want to do this? – Stefan Dollase Feb 25 '16 at 01:52
  • You are welcome. However, you might want to ask a new question for that, since it seems to be quite different from this question. – Stefan Dollase Feb 25 '16 at 02:26
0

Yes you can call main method from another class.

However, you should not be calling the main() method from inside your application. The main() method should be used as an entry point into your application, to launch a program, not be used to recursively execute the logic inside that application. If you have functionality needed again you should put it in a separate method.

sanky jain
  • 873
  • 1
  • 6
  • 14