-1

I've been using the Java IDE - BlueJ but I can't understand why the main method cannot be defined as

public static void main(String[] args)

in it. I get a syntax error when I try it.

Srirag vu
  • 72
  • 1
  • 7
  • And your question isn't clear. Show us what is going wrong, cause what you have mentioned doesn't make much sense. – Debosmit Ray Mar 25 '16 at 04:33
  • Hi Srirag, to maximize the chances of getting a helpful answer, please use the Stackoverflow guideline for creating a minimum, verifiable, complete example. http://stackoverflow.com/help/mcve – entpnerd Mar 25 '16 at 04:34

2 Answers2

2

Read the documentation, e.g. How do I call a main method in BlueJ, and how do I pass it arguments?:

How do I call a main method in BlueJ, and how do I pass it arguments?

You can call a main method in the same way as you call any static method in Java - by right-clicking on the class in the class diagram, and selecting the method from the pop-up menu.

When you call the main method from a class, you will see a parameter entry field that prompts you for the array of strings that the main method takes as a parameter.

By default, the parameter is

{ }

(an empty array, no parameters). If you want to pass, say, three parameters, from a command line you would write

java MyClass one two three

In BlueJ, you use the following parameter for "main" in the dialog text field:

{ "one", "two", "three" }

This passes an array of the three strings, just as the command shell does.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

enter image description here

As you can see, BlueJ throws no Syntax error if you define the main method as public static void main(String[] args). And why should it? That's the standard identity of the main method of a Java program. (If you still get a syntax error, then it'd be helpful if you can add a snap of the window in your question, like I've done in the answer, showing the compiler error.)

In BlueJ, you don't really run the program (just for saying): you just select the method to start executing from by right-clicking on the class Icon. BlueJ is a beginners' IDE. In real life, Java programs are not run the way you do in BlueJ. You can see that if you use IDEs like JCreator or NetBeans. Java programs are executed the way you execute any program on a computer. The .class file is run and the main method is automatically executed by the jre. But for the system to identify the main method, you have to define the main method this way - public static void main(String[] args) - this is the standard manner.

Instead of using BlueJ, if you compile and run a java program in windows cmd using the commands javac and java respectively, you will see the compile-time error saying "main method not found" if you write main() instead of main(String[] args). Below is a snap of what happens in NetBeans IDE when the main method is not defined properly.

enter image description here

And when the perfect program is run in NetBeans,

enter image description here