main() method in java complete explanation - Part 1

Опубликовано: 02 Март 2026
на канале: Learn Java
263
8

You can learn Java with me as the Java Programming language is being made easily. It would take a period of minimum three months and maximum 6 months to master Java. I would recommend you to watch all my videos to crack any software interviews that would require Java Programming language as a primary skill.

The complete picture of main() method in Java:-
Q1) What is the requirement of main() method in Java?
1. To manage application logic in the Java Application , we need main() method.
2. To define starting point and the ending point to get executed, we require main() method.

Filename -Test.java
D:/Java3 javac Test.java

Q2) Could you please let me know if I perform the compilation for the below program would I get any error?

Ans: No, we don't get any compile time error because compiler is going to check for syntax errors not for the main method.
Example:-
class A
{

}
class B{

}
public class Test
{

}

So , when you miss the main method, the error will be thrown in the run time as follows.
D:\Java3java Test
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)

You can place any number of classes in the java file but during the execution, its your responsibility to load the main class name as follows
class A
{
}
class B{
public static void main(String[] args)
{
}
}
class Test
{
}
java MainClassName
Syntax:-
public static void main(String[] args );

Q3) Do you think that the main() method is a user defined one or the predefined one?
predefined? It is not predefined
public static void main(String[] args)
{
//Body is given by the programmer

}
Userdefined? It is not userDefined
public static void main(String[] args );

1. It is neither predefined method nor the user defined method because the main method is always a conventional method with fixed prototype and user defined implementation.


2. The main thread will be created in JVM in such a way that it is recognizing the main method with the prototype as follows
public static void main(String[] args)
{
//Body is given by the programmer
}

public --- static ---- void ----main (String[] args)

Q4) what is the requirement to declare the main method as "public"?

JVM - Main thread- private

You must ensure that the scope must be made available to the JVM and the main thread so that it would get access without any restrictions.

Please be informed that to execute java applications, JVM must be able to access the main method which is already provided by the developer.

To access main() method by JVM , we have to scope the main() method scope to the JVM by declaring it as a "public". Than the access will be granted.

1. If we declare main() method as "private" than main() is available up to the present java class , not available to the JVM which is available in the JVM Software.

2. If we declare main() method as "default" than main() is available up to the present java package , not available to the JVM which is available in the JVM Software.

3. If we declare main() method as "protected" than main() is available up to the present java package and the child classes existed in the other packages ,but not available to the JVM which is available in the JVM Software.

4. If we declare main() method as "public" than main() is available to the other packages or the JVM .So that it could be accessed and executed. That's the reason why the scope of the main method should always be public.


Q5)In java application, if I declare the main method without public would I get compilation error or run time error?

Ans: - There will be no compilation error but the runtime error. Because the compiler is going to perform the syntax checks but the JVM will look for the main method with the specified prototype.
public static void main(String[] args)
Error:-
Error: Main method not found in class B, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Q6) What is the requirement to declare the main() method as static?
As per the JVM internal implementaion, the main method must be static. Because it could be accessed just by using the ClassName. And, it is implemented internally in such a way that it gets accessed by the JVM just by using the respective class name of the main method in the main class.
class A
{
public static void method1()
{
}
}

A.method1();-- It is highly recommended.
A obj =new A();
obj.void method1();
java ClassName
public static void main(String[] args)
{

}

Q7) Below program is valid or in valid with respect to the main method?
static public void main(String[] args)
{
//is it valid or invalid

}

class A
{

}
class B{
static public void main(String[] args)
{
System.out.println("XYZ");

}
}
class Test
{
}

You can interchange the place holders of "public" and "static" keywords in the main method. There will be no errors in the run time. It is completely valid.