java – Why are (String[] args) not used as main method arguments in BlueJ?

java – Why are (String[] args) not used as main method arguments in BlueJ?

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.

enter

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? Thats the standard identity of the main method of a Java program. (If you still get a syntax error, then itd be helpful if you can add a snap of the window in your question, like Ive done in the answer, showing the compiler error.)

In BlueJ, you dont 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

And when the perfect program is run in NetBeans,

enter

java – Why are (String[] args) not used as main method arguments in BlueJ?

Leave a Reply

Your email address will not be published. Required fields are marked *