What is the main method in java ? Explained with example

What is the main method in Java?

If you are a programmer or a computer science student then you must have heard about the popular general-purpose programming language, Java. Today in this article we will discuss in detail the main method in Java.

If you have no knowledge of programming or if you have just started programming in Java and don’t know much about it, then don’t worry.

In this article, we have discussed everything. Starting from JVM (Java virtual machine) to a practical example of using the main method in Java. Read the complete article, and we will solve all your doubts.

Before moving further, let us know a little about Java. Well, Java is a quite popular programming language amongst developers and programmers. It is an object-oriented, class-based programming language that is designed for lesser implementation dependencies.

It is secure, reliable, and a fast programming language, which makes it a top choice for building applications for laptops, mobile phones, game consoles, scientific computers, and much more.

Introduction of JAVA

If you come from a programming language you might know that Java is an object-oriented programming language just like C++, but it is more advanced and has a lot of simplified features.

It is a cross-platform programming language, that makes it ready to run on all platforms.

You may have a doubt, that how Java can run so easily on all platforms. Well, the answer to this is JVM or Java Virtual Machine.

JVM is a virtual machine with which you can execute Java programs as well as programs that are written in other programming languages.

In simple terms, a JVM is like an engine that provides a runtime environment to drive the Java applications or Java code.

This execution of Java programs is done by converting the Java bytecode into machine language. You must be thinking, that why we are telling so much about JVM.

JVM plays an important role in order to understand the main method of Java.

Now that you have learned about Java, and Java Virtual Machine, let us move on to our topic i.e main method in Java.

What is public static void main in java ?

In every program of Java, you must have seen, a syntax or statement was written somewhat like this:

public static void main(String args[])

The term main written in the above statement is the starting point for the Java Virtual Machine to start the execution of the Java program on your device. If the main is not written in your program, the JVM will not be able to execute your Java program.

The main method is predefined in the Java Virtual Machine. When the main method is called, the JVM executes the program line by line and once this method is executed completely, the JVM ends the process of execution.

Let us understand this code written above in more detail. The term public written in the code refers to the access specifier.

Access specifiers refer to the part of code that is visible to the JVM. For example, if it is kept private, then it will not be visible to JVM. Note that, public is a keyword.

The keyword static wrote after the public tells the JVM that to call the main method you do not need any object.

Using the keyword static in a method itself means that the method does not need any object to be called. Such methods can be invoked easily without any need of an object.

In Java, every method has a return type. By writing void to a method, you define that this method or function will be returning no value.

For example, if you create a method that returns an integer value, then that method will be having an int written instead of void.

This shows the compiler that this method will return an integer value.

What if you missed writing String Args?

If by any chance you miss writing String Args in the main method, then the problem is that the code will get compiled but you won’t be able to execute it because the JVM could not recognize the main method.

Not having the main method in your program or application may show you the following error.

What is the main method in java ? Explained with example

Overloading and Overriding main method in Java

Can we overload main method in java ?

Method overloading is defined as a technical component of a class in which the methods of a class have the same name but different parameters.

These parameters may be different in number or type. However, it is not compulsory for every method to have the same return type.

This is known as method overloading and it supports the concept of polymorphism. In Java you can overload the main method.

But in this case, the JVM will only call the original main method and will not call the overloaded main method.

This leads us to solve another query, whether in Java, you can override the main function or not.

Can we overload main method in java?

Method overriding means that if in a class, the methods have the same name as well as the same parameters, then it is known as method overriding.

In this concept of method overriding, one of the classes is a parent class while the other class is a subclass. It is said that the subclass overrides the parent class. In Java, we cannot override the main method.

If you remember we mentioned above that to declare the main method we use a keyword known as static. Due to this keyword, we cannot override the main method in Java.


We hope that we have guided you quite a bit about the main method in Java. Remember that Java is a wonderful language, and a quite diverse one too.

That is why it is in so much demand.  We hope we have solved all your queries about the main method in Java.

Keep coding and subscribe to mytrashcode to read more great content about programming, GUI, and much more.

Leave a Comment

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

We launched our Forever free AI coding platform that Generates, Debugs and Converts code

Scroll to Top