Why Principal Method Is Populace Static Inward Java

Main method inwards Java is the outset programming method a Java programmer knows when he starts learning Java programming language.have you lot always thought most why original method inwards Java is public, static together with void, of-course Yes, since most of us outset larn C together with C++ than nosotros motility to Java inwards our programming path nosotros familiar alongside original method but inwards Java original method is slightly unlike it doesn't render whatever value similar inwards C it returns int, main method is populace static together with void Why? In this post service nosotros volition attempt to notice response to these questions together with convey 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 yell for for whatever heart together with someone Java program. Remember nosotros are non talking most Servlet, MIDlet or whatever other container managed Java plan where life cycle methods are provided to command the execution. In heart together with someone Java program, execution starts from original method when you lot type coffee main-class-name, JVM search for public static void main(String args[]) method inwards that shape together with if it doesn't notice that method it throws mistake NoSuchMethodError:main together with terminates.


Signature of the original method inwards Java
The original method has to strictly follow its syntax; other wise JVM volition non hold upward able to locate it together with your plan volition non run. Here is the exact signature of the original method


public static void main(String args[])

This signature is classic signature together with at that spot from start of Java but alongside introduction of  variable declaration or varargs inwards Java5 you tin shipping away likewise 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 exclusively run inwards Java 1.5 or afterward version. Apart from public, static together with void, at that spot are sure enough keywords similar final, synchronized together with strictfp which are permitted inwards the signature of coffee original method.

Why original method is static inwards Java

 is the outset programming method a Java programmer knows when he starts learning Java prog Why original method is populace static inwards JavaNow come upward to the original yell for "Why the original method is static inwards Java", at that spot are quite a few reasons some but hither are few reasons which brand feel to me:

1. Since the original method is static Java virtual Machine tin shipping away telephone weep upward it without creating whatever illustration of a shape which contains the original method.

2. Since C together with C++ likewise convey similar original method which serves equally entry yell for for plan execution, next that convention volition exclusively attention Java.

3. If original method were non declared static than JVM has to exercise illustration of original Class together with since constructor tin shipping away hold upward overloaded together with tin shipping away convey arguments at that spot would non hold upward whatever sure enough together with consistent way for JVM to notice original method inwards Java.

4. Anything which is declared inwards class inwards Java comes nether reference type together with requires object to hold upward created earlier using them but static method together with static information are loaded into dissever retentivity within JVM called context which is created when a shape is loaded. If original method is static than it volition hold upward loaded inwards JVM context together with are available to execution.

Why original method is populace inwards Java
Java specifies several access modifiers e.g. private, protected together with public. Any method or variable which is declared populace inwards Java tin shipping away hold upward accessible from exterior of that class. Since the original method is populace in
Java, JVM tin shipping away easily access together with execute it.

Why the original method is void inwards Java
Since the original method inwards Java is non supposed to render whatever value, it's made void which but way original is non returning anything.

Summary:
1. The original method must hold upward declared public, static together with void inwards Java otherwise, JVM volition non able to run Java program.

2. JVM throws NoSuchMethodException:main if it doesn't notice the original method of predefined signature inwards shape which is provided to Java command. E.g. if you lot run coffee Helloworld than JVM volition search for populace static void original String args[]) method inwards HelloWorld.class file.

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

4. The original method is run past times a exceptional thread called "main" thread inwards Java. Your Java plan volition hold upward running until your original thread is running or whatever non-daemon thread spawned from the original method is running.

5. When you lot run across "Exception inwards Thread main” e.g.
Exception inwards Thread main: Java.lang.NullPointerException it way Exception is thrown within original thread.

6. You tin shipping away 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 together with public, you lot tin shipping away purpose a final, synchronized together with strictfp modifier inwards the signature of the original method inwards Java.

8. The original method inwards Java tin shipping away hold upward overloaded similar whatever other method inwards Java but JVM volition exclusively telephone weep upward the original method alongside specified signature specified above.

9. You tin shipping away purpose throws clause inwards the signature of the original method together with tin shipping away throw whatever 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 past times JVM.

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

Sumber https://javarevisited.blogspot.com/

0 Response to "Why Principal Method Is Populace Static Inward Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel