1

I am a biggner in java. I am going well with java. The problem is when we declare main function in java as main(String args). I am learning with bluej. It worked fine if I just write main(). So what's the difference between both.

5
  • Sure you can declare it without including String[] args and it will compile fine, but when you run you will get an error. The main entry point must include that signature. Commented Sep 19, 2016 at 16:28
  • Could explain what you mean by "It worked fine if I just write main()"? Do you mean that your code compiles, or that you are able to run code placed inside that main() method? Commented Sep 19, 2016 at 16:32
  • @pshemo it means there was not problem in compiling as well as running the program in bluej Commented Sep 19, 2016 at 16:35
  • 1
    If it really runs then one of options would be that BlueJ is adding public static void main(String[] args){main();} code implicitly to your class. But that behavior is not guaranteed with other IDEs so don't depend on it. Only entry point in Java application is public static void main(String[] args). Commented Sep 19, 2016 at 16:40
  • 2
    Possible duplicate of What is "String args[]"? parameter in main method Java Commented Sep 19, 2016 at 17:55

1 Answer 1

4

public static void main(String[] args is the entry point (which can be final or not, doesn't matter) that the java tool and standard IDEs and such look for in the main class of a Java application. If you don't include the parameter declaration (the String[] args), the signature doesn't match the expectation of the java tool and so may not work.

main() will compile, because it's just a method, but won't work with the java tool and other tools following its conventions.

If BlueJ allows you to leave off the parameter declaration, that's behavior specific to the BlueJ tool.

So for instance, this compiles just fine:

public class Example { public static void main() { System.out.println("Hi"); } } 

It compiles to an Example class with a method called main. But if you try to run that via the java tool:

 $ java Example Error: Main method not found in class Example, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application 

To make it compatible with the java tool, you need the parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

@AkashJaiswal: You'll have to define "it worked." I just realized I'd left an important thing out of the answer and updated. main() will compile, because it's just another method, it just won't work with the java tool or other tools that follow its convention for what the main method signature is. So if by "worked" you mean it ran your main method, that's a BlueJ thign, not standard. If by "worked" you just mean it compiled, well, that's just compiling.
Thanks tj and everyone else
@AkashJaiswal also, just to clarify in case you were wondering what the String[] args means, it is just an array of arguments that you can pass to your program. Usually helpful for developing command line tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.