Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you create a Main class, that is a class that you want to start up when the application begins. It always has a main method when you are running Java from the command line or the console. The syntax for this can differ from one platform to another. For example, you don't use a main method in a Java Servlet, you use other kinds of methods, but when you are running from the command line, it's always called main. The signature or syntax of the main method always looks like this: it has the keywords public, static, and void.
Public means that the method can be called from anywhere. Static means that you don't have to create an instance of the class. And void means that you are not returning anything from the class. Now, I'll talk more about these keywords in later videos, but for now what you need to know is that these three keywords are required as part of the main method declaration. The other thing that the main method has to have is this syntax, String, open/closed bracket, and then the name of the variable, which by default is set to args by Eclipse, but could be anything.
This is a value that's passed in by the Java Virtual Machine as the application starts up. The brackets means that the argument is an array, an ordered collection of values. And these are the command line arguments or values that are passed into the application when it starts up. Now, you can pass arguments in from Eclipse or from the command line. It will make most sense if we start from the command line though. So open up a command window; on Windows use the cmd command and on Mac use terminal, and then change to bin folder underneath the project arguments that I've imported into Eclipse.
I'll type cd Desktop\Exercise Files \03_GettingStarted\Arguments\bin. List the contents of the directory using dir on windows or ls on Mac, and you should see that there is a class there called Main.class. Now, I am going to run the application. First I'll clear my screen. Then I'll type java Main. And I don't see any output, that's because my source code for the moment doesn't have anything in the main method.
I'll switch back to the command window using Alt+Tab on Windows or Command+Tab on Mac, and this time I'll type java Main arg1 arg2. Again, there is no output, but let's add some code to the application to detect and report the arguments. I'll go back to the main method. The args variable is an array, and in Java every array has a property called length, which returns numeric value indicating how many items there are in the array.
So I am going to add a little bit of output that looks like this. System.out.println, and then I'll put two values together, one string and one number. The string will be Number of args: , then I'll move the cursor after the last double quote and put in a Plus operator and then args.length. The length property is numeric. When you take a string and a numeric value and put them together using the plus operator, that's called concatenation, and in Java the numeric value will be automatically converted to a string.
I'll talk about the mechanics of that in a later video. I'll finish the statement with the semicolon at the end and I'll Save my changes. Now, by default when you Save your changes to your code, Eclipse automatically recompiles the application for you. This is because under the Project Menu there is an option labeled Build Automatically. It's checked by default. If you want to you can uncheck that option and only compile or build when you say so, but I'll keep it on, so whenever I Save my changes the .class files are recreated for me.
So now I can switch back to the command window and I'll repeat the same command by pressing the Up Arrow and then either Enter or Return, and I should get the output, Number of args: 2. So now I know that I am receiving the arguments from the command line and that I have exactly the number that I typed in. Let's go back to the code and add a little bit more. Place the cursor after that initial statement and press Enter or Return a couple of times to make a little bit of blank space in your code. Now, I am going to add a little bit of code that loops through the arguments and report the value of each argument in turn.
This takes a little bit more complex code, but the good news is that Eclipse will write the code for you. Try this, type for, the word for, then hold down the Ctrl key and press Space. This keyboard shortcut is the same on both Windows and Mac. It brings up a list of available options. Choose the first option, for - iterate over array by pressing Enter or Return, and Eclipse adds a bunch of code that will loop through the array. Now, I am not going to describe the exact syntax of this code right now, we'll save that for later, but all you need to know is that when you use a for command and you provide all this information, you are looping through the contents of the declared array, in this case args, and you can deal with each item of the array in turn.
I'll click into the for loop, between the braces, and once again I'll use System.out.println and then I'll pass in the value of the current argument using the syntax args, open bracket, i, closed bracket, and I'll remember to put in my semicolon at the end. i is a variable that was declared inside the for loop, when I said int i = 0, it's initial value is 0, and each time through the loop it's incremented by 1.
The first item in the array is item 0, the second one is item 1, and so on. I'll press Command+S or Ctrl+S to save my changes, switch back to the command line, and pressing the Up Arrow run the application again. And now I see the total number of arguments and I see the value of each argument output one at a time. When you pass arguments from the command line, each argument is delimited and separated from the other by a space character, but if you want to include a space character and a value, just wrap the value inside quotes.
So I'll press the Up Arrow to bring that command back again, but I'll move the cursor over and add quotes around the two words, arg1 and arg2. Then I'll run the application again, and this time I am told I only have one argument, and the value of the argument is the entire string. So that's how you pass in the arguments from the command line. How would you do the same thing directly in Eclipse? I'll go back to Eclipse and I'll go into my Run Configurations. I'll go to the Toolbar, pull down the Run Menu and choose Run Configurations.
I'll add a new Run Configuration by clicking Java Application and clicking Plus, and the new name is going to be Main (1). And over here I'll click on the Arguments tab. I can pass in as many arguments as I want. I'll list these arguments one per line, so this will be arg1 and arg2 and arg3 and arg4. Then I'll click the Run button, and down here in the Console View I'll expand the Console so we can see all the output.
I see Number of args: 4 and then each of the arguments listed one at a time as the code loops through and deals with each item in turn. So that's the purpose of the args parameter that's passed into the main method. It allows you to receive and process values that are passed into your Java application at runtime, either on the command line working in the command application in Windows, or in terminal on Mac, or from within Eclipse from a Run Configuration that's set up for this purpose.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97508 Viewers
61 Video lessons · 84747 Viewers
71 Video lessons · 68923 Viewers
56 Video lessons · 101377 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.