Role of Main Method

Role of Main Method

Hello to my all Readers!....

Hope You All are having good Reading

Below I'm sharing my weekly learning regarding Technical Concept (Java Language). Have a glance on this and enjoy your reading.

The most important method in java i.e "Main Method"

What is main method?

  • Main method is a user defined method with predefined prototype syntax.

Syntax of main method?

public static void main(String[] args);
No alt text provided for this image

As per the rule method should be called so, main method automatically executing?

  • JVM internally contain main method calling in it. Therefore Execution of a program starts with main method.

-----------------------------------------------------------------

What is the use of main method?

  • Main method is used for starting the execution of an application as standalone application by using the command java.
  • Main method is the initial point of a class execution. It means JVM will start class logic execution with main method And ends with main method.

------------------------------------------------------------------

If Class doesn't have main method?

Class will not execute and JVM will throw error. that is we can compile the class but at the time of execution we will be getting error. Therefore main method is mandatory for?execution only. For just compiling a class main method isn't required.

No alt text provided for this image
No alt text provided for this image

----------------------------------------------------------------------

Can we change the parts of main method?

Except "args" we aren't allowed to change or remove the main method other parts. We will get either CE or RE.

  • In place of args we can place any name Ex:- Krish.

???????????public static void main(String krish[]);
No alt text provided for this image

  • We can use [] either after string, before?args and after args, but not before string.

???????????public static void main(String args[]);
??public?static void main(String[] args);
?

  • We can use … 3dot's in place of [] from java 1.5v onwards.

public static void main(String… args);

? String...args:?It allows the method to accept zero or multiple arguments. There should be exactly three dots?between String and array; otherwise, it gives an error.

No alt text provided for this image

  • We can use public?and static in any order but must be used before void.

Public static void main(String[] args);
static public void main(String[] args);
No alt text provided for this image

------------------------------------------------------------------

Why main method has public?

  • To be accessed by JVM software from outside of our project directory, main method must has public keyword. We should use a public keyword before the main() method so that JVM can identify the execution point of the program. If we use private, protected, and default before the main() method, it will not be visible to JVM.

------------------------------------------------------------------

Why main method has static?

  • To access and execute main method logic without creating object, main method must have static keyword. Static methods are the method which invokes without creating the objects, so we do not need any object to call the main() method.?

---------------------------------------------------------------------

Why main method has void?

  • Returning value from the main method is?useless because the value returning from main method will be return to JVM, JVM will not allow to any work with this returned value. Hence to stop returning value from main method , main?method?return type must be void.

------------------------------------------------------------------------

Why main method name is main?

  • According to coding standards main method name is chosen as main because of the initial point of class execution. And moreover the name main is conversional name derived from C and C++, because java is descendent of C and C++ language.

-------------------------------------------------------------------------

Why main method has parameter?

  • Main method has parameter to receive input values from enduser at program execution time from command line.

-------------------------------------------------------------------------

Why main method parameter type is array?

  • To allow enduser to pass multiple values.

? --------------------------------------------------------------------------

Why main method parameter type is string[] ?

  • Any value sending from keyword will passed into a program as string type by OS, and moreover any values can be represented in terms of string and further it can be converted to its original type. Hence SUN chosen main method parameter type as String[].

-------------------------------------------------------------------------------?

Why main method parameter name is args?

  • The input values sending to a method parameter is technically called as argument, so it is the short-cut name of argument.

? -----------------------------------------------------------------------------------

Can we call main method explicitly?

  • Yes, it is possible because it is also one of method type. But main() method should not be called from its own block or from a method that is calling from main(); it leads to exception java.lang.StackOverFlowError.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

-----------------------------------------------------------------------------------

What happens if the main() method is written without String args[]?

  • The program will compile, but not run, because JVM will not recognize the main() method. Remember JVM always looks for the main() method with a string type array as a parameter.

?Execution Process

  • First, JVM executes the static block, then it executes static methods, and then it creates the object needed by the program. Finally, it executes the instance methods. JVM executes a static block on the highest priority basis. It means JVM first goes to static block even before it looks for the main() method in the program.

No alt text provided for this image

  • We observe that JVM first executes the static block, if it is present in the program. After that it searches for the main() method. If the main() method is not found, it gives error.

No alt text provided for this image
No alt text provided for this image

??

Thankyou For Reading!

Published by:?Sakshi Agrawal

Date?:?March 30,2022


?


?

要查看或添加评论,请登录

Sakshi Agrawal的更多文章

  • SpringBoot Annotation

    SpringBoot Annotation

    Hello, to my all readers, Hope you all are enjoying your daily learning. Among my daily updates, Today I have come up…

  • Inner V/s Sub Classes In Java

    Inner V/s Sub Classes In Java

    Hello, to my all readers, Hope you all are enjoying your daily learning. Among my daily updates, Today I have come up…

  • What’s the variance amid an Abstract Class and Interface in Java?

    What’s the variance amid an Abstract Class and Interface in Java?

    Greetings to all my esteemed readers! I trust that each of you is enjoying a delightful reading experience. Please find…

  • Bitbucket

    Bitbucket

    Hello to my all Readers!..

  • Microservices

    Microservices

    Hello to my all Readers!..

  • My New Journey

    My New Journey

    Hello to my all Readers, Hope you are enjoying your reading. It's being quit long time I haven't shared the thoughts…

    2 条评论
  • Programs on operators

    Programs on operators

    Hello to all my Reader As you see my daily updates regarding my new work as a Python faculty in CCCGadarwara [Christ…

  • Python Session Overview

    Python Session Overview

    Hello to all my Reader As you see my daily updates regarding my new work as a Python faculty in CCCGadarwara [Christ…

  • Python Session Overview

    Python Session Overview

    Hello to all my Reader As you see my daily updates regarding my new work as a Python faculty in CCCGadarwara [Christ…

  • Python Session Overview

    Python Session Overview

    Hello to all my Reader As you see my daily updates regarding my new work as a Python faculty in CCCGadarwara [Christ…

社区洞察

其他会员也浏览了