Class method main

In your application program that you create using the Java language, you will write classes. A class has one class definition composed of methods and statements. Your application might be composed of one class or many classes. It depends on the complexity of the application. A class is a blueprint of an object. You create objects that interact with each other to have your application complete the desired tasks needed.


There is a special method that is used in Java to determine where the Java Virtual Machine will begin executing methods and statements. This method is called the main method. Every class definition may only have one main method. Typically, your program application will contain only one main method for the entire program. An application only requires one way to start working and that is called the main method when programming in the Java language.

Here is an example of the main method:

public static void main (String[] args)
     // code goes here

public - the access level of the main method. It is public. It is usable by everyone. If the main method was any other access level, no one would be able to use your application program. Therefore, the main method is always considered public.

static - is a java keyword that means that the method belongs to the class not the object of the class. You do not have to "create" an object to use this method. A method that is not static can only be used if you construct an object first. With the keyword static, you can use the method without constructing the object. This is important because, considering it is the entry point to your program, you don't want it difficult to start your program. Therefore, it's static.

void - means that once your application program has completed running that it does not return any information. It just ends the program nicely. 

main - is the name of the method. It's the main method.

(String[] args) - This is a tricky piece of the code. The "half-moons" designate what data goes into the method. We call this arguments to the method. Any arguments to the main method must be given as an array of String values called args. Don't get  worried about the details of this part of the main method. You will always write it as you see it here. Further understanding of this piece of code will come relevant when you understand arrays and Strings. For now, just understand it always is exactly as you see it.

{} - the opening and closing brackets for the method. 


This is enough information to create your very first Java application. We will be traditional and create the HelloWorld application. In programming, everyone always teaches the HelloWorld example. We will create one class definition. It will contain one method and one statement.  I am not going to break down this program as to how it works. Study this little program based on what you have learned. At this point, this program should make sense. The statement should be the only thing that you do not recognize. When you run the program, you will be able to guess what the statement does. Give it a try.

public class HelloWorld
     * Make sure and save your file with this filename
     * Compile the program using your Integrated Development Environment for Java
     * Run your program

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


Congrats! You have wrote your first Java program!