Why is the main() method declared static in Java?

CWC
3 Min Read

Principle method in Java is the primary programming process a Java programmer knows when he begins learning Java programming language. Have you at any point considered why principle method in Java is public, static and void? Apparently Yes. Since the vast majority of us initially learn C and C++, then we move to Java in our programming way that we are familiar with principle method yet in Java basic method is somewhat different it doesn’t give back any value.

Why is the main() method declared static in Java?

There are many reasons around however here are few ideas which sound good to me:

  1. Since the primary method is static Java VM can call it without making any instance of a class which contains the principle method.
  2. Since C and C++ additionally have comparable principle way which serves as a passage point for program execution, taking after that convention will just help Java.
  3. If the fundamental method were not declared static than JVM needs to make an instance of first Class and since constructor which may be overloaded and may have arguments there would not be a sure and consistent path for JVM to discover principle method in Java.
  4. Anything which is declared in class in Java goes under reference sort and obliges object to be created before utilizing them yet static method, and static information is loaded into separate memory inside JVM called context that is created when a class is loaded. If principle method is static, then it will be loaded in JVM context and are accessible to execution.

Java specifies a few access modifiers e.g. private, secured and public. Any method or variable which is declared open in Java can be available from outside of that class. Since the first method is public in Java, JVM can run without much of a stretch access and execute it. Since the primary method in Java is not supposed to give back any value, it’s made void which just means principle is not returning anything.

Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version