- main method is a standard method used by JVM to execute any java program(Java SE).
- Lets see an example class without main method.
- package instanceofjava;
- public Demo{
- }
- This class will compile fine but while executing JVM fails to find the main method in that class
- So it will throw an exception: NoSuchMethodError:main
- package instanceofjava;
- public Demo{
- public static void main(String [] args){
- }
- }
- This program will compile and executed. Because at run time JVM calls the main method on that class.
Why main method is public?
- To call by JVM from anywhere main method should be public.
- If JVM wants to call our method outside of our package then our class method should be public.
- package instanceofjava;
- public MainDemo{
- public static void main(String [] args){
- }
- }
public static void main(string args) interview Questions
Why main method is static?
- To execute main method without creating object then the main method oshould be static so that JVM will call main method by using class name itself.
Why main method return type is void?
- main method wont return anything to JVM so its return type is void.
Why the name main?
- This is the name which is configured in the JVM.
Why the arguments String[] args?
- Command line arguments.
- String array variable name args : by the naming conventions they named like that we can write any valid variable name.
Can we write static public void main(String [] args)?
- Yes we can define main method like static public void main(String[] args){}
- Order of modifiers we can change.
- package instanceofjava;
- public MainDemo{
- static public void main(String [] args){
- }
- }
What are the possible public static void main(String [] args)method declaration with String[] args?
- public static void main(String[] args){ }
- public static void main(String []args){ }
- public static void main(String args[] ){ }
- public static void main(String... args){ }
- Instead of args can place valid java identifier.
Can we declared main method with final modifier?
- Yes we can define main method with final modifier.
- package instanceofjava;
- public MainDemo{
- public static final void main(String [] args){
- }
- }
Main method with all possible modifiers:
- package instanceofjava;
- public MainDemo{
- static final synchronized public void main(String [] args){
- System.out.println("Main method");
- }
- }
Output:
- Main method
Overloading main method:
- We can declare multiple main methods with same name as per overloading concept.
- So we can overload main method but every time JVM looks for main method with String[] args method.
- Lets see an example program on main method overloading.
- main(String[] args) method will be called automatically by JVM.
- If we want to call other methods we need to call explicitly.
- package instanceofjava;
- public MainDemo{
- public static void main(String [] args){
- System.out.println("Main method String [] args");
- }
- public static void main(int[] args){
- System.out.println("Main method int[] args");
- }
- }
Output:
- Main method String [] args
Main method Inheritance:
- Inheritance concept is applicable for main method.
- If a sub class not having main method and extending a super class which is having main method.
- Then if we execute super class main method will be executed fine.
- If we execute sub class which is not having main method also executed fine and super class main method will be called.
- package instanceofjava;
- public A{
- public static void main(String [] args){
- System.out.println("Main method of class A");
- }
- }
Output:
- Main method of class A
- package instanceofjava;
- public B extends A{
- }
- Main method of class A
What happen if both super and sub class having main method?
- Actually its not overriding concept here. it is a method hiding concept in this scenario regarding with main method.
- package instanceofjava;
- public A{
- public static void main(String [] args){
- System.out.println("Main method of class A");
- }
- }
Output:
- Main method of class A
- package instanceofjava;
- public B extends A{
- public static void main(String [] args){
- System.out.println("Main method of class B");
- }
- }
Output:
- Main method of class B
A nice content. We can know many things from here. Keep going to write many things like this.
ReplyDeleteThank You.
You may visit this site Valobashar Sob Rong
Many of my ambiguities are clarified after reading this page. Thank you so much.. :)
ReplyDeletenice answer and explain very clearly.thank u so much.....
ReplyDeleteMain method Inheritance:If we execute sub class which is not having main method also executed fine and super class main method will be called.I think it subclass wont be executable code without main method.
ReplyDelete