Why Principal Method Is World Static Inwards Java

Advertisement

Masukkan script iklan 970x90px

Why Principal Method Is World Static Inwards Java

Jumat, 29 Mei 2020

Main method inwards Java is the showtime programming method a Java programmer knows when he starts learning Java programming language.have y'all e'er thought virtually why original method inwards Java is public, static in addition to void, of-course Yes, since most of us showtime larn C in addition to C++ than nosotros motion to Java inwards our programming path nosotros familiar amongst original method but inwards Java original method is slightly dissimilar it doesn't render whatsoever value similar inwards C it returns int, main method is world static in addition to void Why? In this postal service nosotros volition attempt to uncovering response to these questions in addition to bring an thought of i of the most pop questions inwards Java why original method is declared Static.

What is the original method inwards Java?

Main method inwards Java is entry betoken for whatsoever inwardness Java program. Remember nosotros are non talking virtually Servlet, MIDlet or whatsoever other container managed Java programme where life bike methods are provided to command the execution. In inwardness Java program, execution starts from original method when y'all type coffee main-class-name, JVM search for public static void main(String args[]) method inwards that degree in addition to if it doesn't uncovering that method it throws fault NoSuchMethodError:main in addition to terminates.


Signature of the original method inwards Java
The original method has to strictly follow its syntax; other wise JVM volition non endure able to locate it in addition to your programme volition non run. Here is the exact signature of the original method


public static void main(String args[])

This signature is classic signature in addition to at that topographic point from start of Java but amongst introduction of  variable declaration or varargs inwards Java5 you tin strength out equally good declare the original method inwards Java using varargs syntax equally shown inwards below example:

public static void main(String... args)

Remember varargs version of coffee original method volition alone operate inwards Java 1.5 or after version. Apart from public, static in addition to void, at that topographic point are for sure keywords similar final, synchronized in addition to strictfp which are permitted inwards the signature of coffee original method.

Why original method is static inwards Java

 is the showtime programming method a Java programmer knows when he starts learning Java prog Why original method is world static inwards JavaNow come upwards to the original betoken "Why the original method is static inwards Java", at that topographic point are quite a few reasons to a greater extent than or less but hither are few reasons which brand feel to me:

1. Since the original method is static Java virtual Machine tin strength out telephone phone it without creating whatsoever event of a degree which contains the original method.

2. Since C in addition to C++ equally good bring similar original method which serves equally entry betoken for programme execution, next that convention volition alone aid Java.

3. If original method were non declared static than JVM has to exercise event of original Class in addition to since constructor tin strength out endure overloaded in addition to tin strength out bring arguments at that topographic point would non endure whatsoever for sure in addition to consistent way for JVM to uncovering original method inwards Java.

4. Anything which is declared inwards class inwards Java comes nether reference type in addition to requires object to endure created earlier using them but static method in addition to static information are loaded into divide retention within JVM called context which is created when a degree is loaded. If original method is static than it volition endure loaded inwards JVM context in addition to are available to execution.

Why original method is world inwards Java
Java specifies several access modifiers e.g. private, protected in addition to public. Any method or variable which is declared world inwards Java tin strength out endure accessible from exterior of that class. Since the original method is world in
Java, JVM tin strength out easily access in addition to execute it.

Why the original method is void inwards Java
Since the original method inwards Java is non supposed to render whatsoever value, it's made void which only agency original is non returning anything.

Summary:
1. The original method must endure declared public, static in addition to void inwards Java otherwise, JVM volition non able to run Java program.

2. JVM throws NoSuchMethodException:main if it doesn't uncovering the original 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 world static void original String args[]) method inwards HelloWorld.class file.

3. The original method is an entry betoken for whatsoever Core Java program. Execution starts from the original method.

4. The original method is run yesteryear a particular thread called "main" thread inwards Java. Your Java programme volition endure running until your original thread is running or whatsoever non-daemon thread spawned from the original method is running.

5. When y'all run into "Exception inwards Thread main” e.g.
Exception inwards Thread main: Java.lang.NullPointerException it agency Exception is thrown within original thread.

6. You tin strength out declare the original method using varargs syntax from Java 1.5 onwards e.g.
public static void main(String... args)

7. Apart from static, void in addition to public, y'all tin strength out role a final, synchronized in addition to strictfp modifier inwards the signature of the original method inwards Java.

8. The original method inwards Java tin strength out endure overloaded similar whatsoever other method inwards Java but JVM volition alone telephone phone the original method amongst specified signature specified above.

9. You tin strength out role throws clause inwards the signature of the original method in addition to tin strength out throw whatsoever checked or unchecked Exception.

10. A static initializer block is executed fifty-fifty earlier JVM calls the original method. They are executed when a Class is loaded into Memory yesteryear JVM.

Further Learning
Complete Java Masterclass
How to Split String inwards Java Program