Why Mind Method Is Populace Static Inwards Java
Main method inwards Java is the showtime programming method a Java programmer knows when he starts learning Java programming language.have y'all always thought nearly why master copy method inwards Java is public, static as well as void, of-course Yes, since most of us showtime acquire C as well as C++ than nosotros motion to Java inwards our programming path nosotros familiar amongst master copy method but inwards Java master copy method is slightly dissimilar it doesn't render whatsoever value similar inwards C it returns int, main method is populace static as well as void Why? In this post nosotros volition attempt to uncovering respond to these questions as well as convey an thought of 1 of the most pop questions inwards Java why master copy method is declared Static.
What is the master copy method inwards Java?
Main method inwards Java is entry signal for whatsoever heart Java program. Remember nosotros are non talking nearly Servlet, MIDlet or whatsoever other container managed Java programme where life bicycle methods are provided to command the execution. In heart Java program, execution starts from master copy method when y'all type coffee main-class-name, JVM search for public static void main(String args[]) method inwards that degree as well as if it doesn't uncovering that method it throws mistake NoSuchMethodError:main as well as terminates.
Signature of the master copy method inwards Java
The master copy method has to strictly follow its syntax; other wise JVM volition non last able to locate it as well as your programme volition non run. Here is the exact signature of the master copy method
public static void main(String args[])
This signature is classic signature as well as at that spot from start of Java but amongst introduction of variable declaration or varargs inwards Java5 you tin forcefulness out too declare the master copy method inwards Java using varargs syntax every bit shown inwards below example:
public static void main(String... args)
Remember varargs version of coffee master copy method volition alone operate inwards Java 1.5 or subsequently version. Apart from public, static as well as void, at that spot are for sure keywords similar final, synchronized as well as strictfp which are permitted inwards the signature of coffee master copy method.
Why master copy method is static inwards Java
Now come upward to the master copy signal "Why the master copy method is static inwards Java", at that spot are quite a few reasons unopen to but hither are few reasons which brand feel to me: 1. Since the master copy method is static Java virtual Machine tin forcefulness out telephone telephone it without creating whatsoever event of a degree which contains the master copy method.
2. Since C as well as C++ too convey similar master copy method which serves every bit entry signal for programme execution, next that convention volition alone assistance Java.
3. If master copy method were non declared static than JVM has to practise event of master copy Class as well as since constructor tin forcefulness out last overloaded as well as tin forcefulness out convey arguments at that spot would non last whatsoever for sure as well as consistent way for JVM to uncovering master copy method inwards Java.
4. Anything which is declared inwards class inwards Java comes nether reference type as well as requires object to last created earlier using them but static method as well as static information are loaded into dissever retention within JVM called context which is created when a degree is loaded. If master copy method is static than it volition last loaded inwards JVM context as well as are available to execution.
Why master copy method is populace inwards Java
Java specifies several access modifiers e.g. private, protected as well as public. Any method or variable which is declared populace inwards Java tin forcefulness out last accessible from exterior of that class. Since the master copy method is populace in
Java, JVM tin forcefulness out easily access as well as execute it.
Why the master copy method is void inwards Java
Since the master copy method inwards Java is non supposed to render whatsoever value, it's made void which only way master copy is non returning anything.
Summary:
1. The master copy method must last declared public, static as well as void inwards Java otherwise, JVM volition non able to run Java program.
2. JVM throws NoSuchMethodException:main if it doesn't uncovering the master copy method of predefined signature inwards degree which is provided to Java command. E.g. if y'all run coffee Helloworld than JVM volition search for populace static void master copy String args[]) method inwards HelloWorld.class file.
3. The master copy method is an entry signal for whatsoever Core Java program. Execution starts from the master copy method.
4. The master copy method is run past times a particular thread called "main" thread inwards Java. Your Java programme volition last running until your master copy thread is running or whatsoever non-daemon thread spawned from the master copy method is running.
5. When y'all run across "Exception inwards Thread main” e.g.
Exception inwards Thread main: Java.lang.NullPointerException it way Exception is thrown within master copy thread.
6. You tin forcefulness out declare the master copy method using varargs syntax from Java 1.5 onwards e.g.
public static void main(String... args)
7. Apart from static, void as well as public, y'all tin forcefulness out role a final, synchronized as well as strictfp modifier inwards the signature of the master copy method inwards Java.
8. The master copy method inwards Java tin forcefulness out last overloaded similar whatsoever other method inwards Java but JVM volition alone telephone telephone the master copy method amongst specified signature specified above.
9. You tin forcefulness out role throws clause inwards the signature of the master copy method as well as tin forcefulness out throw whatsoever checked or unchecked Exception.
10. A static initializer block is executed fifty-fifty earlier JVM calls the master copy method. They are executed when a Class is loaded into Memory past times JVM.
Further Learning
Complete Java Masterclass
How to Split String inwards Java Program
Komentar
Posting Komentar