Where we take good care of both your front and back-end needs

Great place to take your code to the next level

Checkout my work and get in touch

RastcodeS Logo
Go Back

Java Break - Main Method - Java Core Part - II


Written by Rastko

date - 26/Aug/18 | views - 738

Java Break - Main Method - Java Core Part - II

     Exploring Java main method. Explanation of the main method's signature, how to compile java code and run it.

     This is the second article in the Java Break series, where we talk about core Java features at great lengths. If you want to read the first one, where we explored Java class structure, be sure to check out the link bellow:

Java Break - Class Structure - Java Core Part - I




  • No prior knowledge necessary, complete beginners are welcome



  • JDK - Java Development Kit
    • You can get it at - Java Development Kit

    • Additional step includes setting a system variable path:
      • You can do this by going to Control Panel - System and Security - System - Advanced system settings

      • Click on Environment Variables and edit Path under System variables.

      • !!! Important !!! - you do not want to overwrite the path, just add to it:

      • C:\Program Files\Java\jdk1.8.0_111\bin;

      • Note - JDK version is just an example. You have to add yours. You can check that out under C - Program Files - Java

  • Any text editor - you can use Notepad

  • You'll also need a command line interpreter
    • I'm using a built in Command Prompt. If you're on a MAC, you can use the Terminal.


Java Main Method


     Java programs begin their run cycle with the main method. The JVM ( Java Virtual Machine ) then handles memory allocation, CPU time ...

     Here's a simple example of a java class with a main method:

public class FirstJavaProject {
   public static void main(String[] args) {
      System.out.println("Hello World!");


     If you want to follow along, create a folder where you'll place the tutorial files. I named mine FirstJavaProject. Inside it create a file FirstJavaProject.java - where you'll add the code above.

     Let's explore this class. As you can see, we declare a public class named FirstJavaProject. It holds only one method - the main method. Ok, there's a lot included in the main method's declaraion. We're going to go though it step by step style.

     The first bit is the public keyword, we already talked about. It's an access modifier. It means anything can access it. We'll talk about other access modifiers in later examples, just know that the main method has to be public.

     The second part is a bit more complex. Static is also a keyword. It means a member, in this case a method is attached to the class and can be called, using just the class name. For our example it would look something like:



     Because of this, java doesn't need to make a new object to run the method. In the case of a main method the JVM automatically executes it. If the main method isn't present, the JVM throws an error and stops the running process.

    This code would produce an error:

public class NoMain {	


Error: Main method not found in class NoMain, please define the main method as:
public static void main(String[] args)


     As you can see, java expects exactly what we placed in our FirstJavaProject class, nothing more, nothing less.

     As we've talked earlier, void is a return type. Well, it actually means there's nothing to return. Main method can only be void.

     Next, we get to the name - main, which needs to be called that in order for JVM to run it automatically.

     Inside the parenthesis, we have the parameter list. In the case of a main method, it's always an array, marked by the [], of String objects. Now, how you write this can vary a little. Instead of String[] args, you may use Strigng args[] and String... args. All will behave the same. We'll try out the parameters later on.

     Finally, within the curly braces, we have the method body. In our case, we're just printing Hello World!. Hello World is kind of a tradition when writing first code in a particular language. Method body can be empty, it just wouldn't do anything.

     It's time to compile and run our first code.


Compiling and Running Java Code


     Open command prompt from a folder, containing FirstJavaProject.java, or navigate to it.

     To compile the java file run:

javac FirstJavaProject.java


     If you now look inside the folder - you'll see another file - FirstJavaProject.class. The insides might look confusing. This is bytecode - it has all the information JVM needs to execute our code.

     Now we're going to run this file:

java FirstJavaProject


     If nothing went wrong and you have correctly set up the environment, you'll see the expected output:

Hello World!


     In order to ensure the code runs correctly remember two things:

     A java file should contain one class. This isn't really true, but we'll cover that later.

     The file name should match the name of the class.

     Amazing, that's your first Java program.


Main Method Parameters


     Executing the main method allows us to add parameters, which our code can use. To show this, we'll create a new file - MainParameters.java:

public class MainParameters {
   public static void main(String[] args) {


     You might be wandering, what the method body does.

     The args[0], args[1] ... is how we access array parts. So here we're just going to print the first two parameters. Take note that arrays in Java are so called 0 indexed. This means their index starts from 0 and goes on incrementing by 1.

     Let's first compile this code:

javac MainParameters.java


     Ok, now that we have our class file, let's run it and break it:

java MainParameters


     Woah, we get a nasty exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MainParameters.main(MainParameters.java:3)


     This exception is very useful, as it tells us where we made a mistake - you see line 3.

     In line 3, we are try to access the first member of the args array and we haven't provided any parameters while running our code. That's what ArrayIndexOutOfBoundsException is trying to tell us.

     Let's fix that by running our little program properly:

java MainParameters Bob John


     Success, we get our expected output:




     Bob and John were our parameters an that's exactly what we need to provide our main method in order for it to run properly.


Classes and Files


     I believe I owe you all a bit of an explanation regarding the "one class per file" rule we mentioned before. In reality, we can put multiple classes in a single file, but not without restrictions.

     Only one of these classes may be public.

     That public class needs to match the file name.

     We'll cover other access modifiers later, as I promised. Also, I suggest you keep the non-official rule of "one class per file", until you get more comfortable with advanced stuff.


     That's all for this installation of getting to know Java. Next we'll be exploring packages and imports. See you in the next Java Break.

View Post/Backend
Login to comment


No Comments yet. Be the first!

Currently working on a Laravel project

Check it out
Java Break - Declare and Initialize Variables - Java Core Part - VI
Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter...