Simple java main example

Java main() method explained with examples

In this article, we will learn Java main() method in detail. As the name suggest this is the main point of the program, without the main() method the program won’t execute.

What is a main() method in Java?

The main() method is the starting point of the program. JVM starts the execution of program starting from the main() method.

Syntax of main() method:

public static void main(String args[])

public: We have already learned in the access specifier tutorial that public access specifier allows the access of the method outside the program, since we want the JVM to identify the main method and start the execution from it, we want it to be marked “public”. If we use other access modifier like private, default or protected, the JVM wouldn’t recognise the main() method and the program won’t start the execution.

static: The reason the main() method is marked static so that it can be invoked by JVM without the need of creating an object. In order to invoke the normal method, we need to create the object first. However, to invoke the static method we don’t need an object. Learn more about static method here.

Читайте также:  Php javascript button click

void: This is the return type. The void means that the main() method will not return anything.

main(): This the default signature which is predefined by JVM. When we try to execute a program, the JVM first identifies the main() method and starts the execution from it. As stated above, the name of this method suggests that it is the “main” part of the program.

String args[]: The main method can also accepts string inputs that can be provided at the runtime. These string inputs are also known as command line arguments. These strings inputs are stored in the array args[] of String type.

Can we have main() method defined without String args[] parameter?

If we have a main() method without String args[] in a program, the program will throw no compilation error however we won’t be able to run the program as the JVM looks for the public main method with the String args[] parameter and if it doesn’t find such method, it doesn’t run the program.

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

As you can see that the program threw error at runtime.

Java – static block vs main method

As we learned in the previous article, static block is used to initialise the static data members. Let’s run a program with static block and main method (static method) to see in which order they run.

class JavaExample < //static block static < System.out.println("Static Block"); >//static method public static void main(String args[]) < System.out.println("Main Method"); >>

As we can see, the static block executed before the main method.

What if a program doesn’t have a main method?

Let’s write a program without the main method to see whether it runs or not.

Error: Main method not found in class JavaExample, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Different ways to write main method in java

The following are the valid ways to write a main method in java:

public static void main(String[] args) //We can interchange static and public static public void main(String[] args) //We can place the square brackets at the different locations public static void main(String[] args) public static void main(String []args)

Overloading of main() method in Java

We can overload the main method in Java. This allows us to have more than one main() method in Java. However the signature of all the overloaded methods must be different. To learn more about overloading, refer this guide: Method overloading in Java.

class JavaExample < public static void main(String args[]) < System.out.println("main method"); main(100); main('A'); >//Overloaded int main method public static void main(int a) < System.out.println(a); >//Overloaded char main method public static void main(char ch) < System.out.println(ch); >>

Frequently Asked Questions

The main method is used to specify the starting point of the program. This is the starting point of our program from where the JVM starts execution of the program.

No, you can’t declare a method inside main() method.

Yes we have can more than one main methods in java, however JVM will always calls String[] argument main() method. Other main() methods will act as a Overloaded method. In order to invoke these overloaded methods, we have to call them explicitly.

No, we cannot override main method of java because it is a static method and we cannot override a static method. The static method in java is associated with class which is why we don’t need an object to call these. Therefore, it is not possible to override the main method in java.

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Lesson: A Closer Look at the «Hello World!» Application

Now that you’ve seen the «Hello World!» application (and perhaps even compiled and run it), you might be wondering how it works. Here again is its code:

The «Hello World!» application consists of three primary components: source code comments, the HelloWorldApp class definition, and the main method. The following explanation will provide you with a basic understanding of the code, but the deeper implications will only become apparent after you’ve finished reading the rest of the tutorial.

Source Code Comments

/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp < public static void main(String[] args) < System.out.println("Hello World!"); // Display the string. > >

Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments:

/* text */ The compiler ignores everything from /* to */ . /** documentation */ This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */ . The javadoc tool uses doc comments when preparing automatically generated documentation. For more information on javadoc , see the Javadoc™ tool documentation . // text The compiler ignores everything from // to the end of the line.

The HelloWorldApp Class Definition

/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp public static void main(String[] args) < System.out.println("Hello World!"); // Display the string. >> 

As shown above, the most basic form of a class definition is:

The keyword class begins the class definition for a class named name , and the code for each class appears between the opening and closing curly braces marked in bold above. Chapter 2 provides an overview of classes in general, and Chapter 4 discusses classes in detail. For now it is enough to know that every application begins with a class definition.

The main Method

/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp < public static void main(String[] args) System.out.println("Hello World!"); //Display the string. > >

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order ( public static or static public ), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose «args» or «argv».

The main method is similar to the main function in C and C++; it’s the entry point for your application and will subsequently invoke all the other methods required by your program.

The main method accepts a single argument: an array of elements of type String .

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application. For example:

Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:

The «Hello World!» application ignores its command-line arguments, but you should be aware of the fact that such arguments do exist.

System.out.println("Hello World!");

uses the System class from the core library to print the «Hello World!» message to standard output. Portions of this library (also known as the «Application Programming Interface», or «API») will be discussed throughout the remainder of the tutorial.

Источник

Оцените статью