Why Principal Method Is World Static Inward Java

Main method inwards Java is the get-go programming method a Java programmer knows when he starts learning Java programming language.have you lot always thought nigh why top dog method inwards Java is public, static together with void, of-course Yes, since most of us get-go larn C together with C++ than nosotros motility to Java inwards our programming path nosotros familiar amongst top dog method but inwards Java top dog method is slightly dissimilar it doesn't render whatever value similar inwards C it returns int, main method is world static together with void Why? In this post service nosotros volition endeavor to uncovering respond to these questions together with convey an thought of i of the most pop questions inwards Java why top dog method is declared Static.

What is the top dog method inwards Java?

Main method inwards Java is entry signal for whatever nitty-gritty Java program. Remember nosotros are non talking nigh Servlet, MIDlet or whatever other container managed Java plan where life wheel methods are provided to command the execution. In nitty-gritty Java program, execution starts from top dog method when you lot type coffee main-class-name, JVM search for public static void main(String args[]) method inwards that cast together with if it doesn't uncovering that method it throws mistake NoSuchMethodError:main together with terminates.


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


public static void main(String args[])

This signature is classic signature together with in that location from start of Java but amongst introduction of  variable declaration or varargs inwards Java5 you tin likewise declare the top dog method inwards Java using varargs syntax every bit shown inwards below example:

public static void main(String... args)

Remember varargs version of coffee top dog method volition alone run inwards Java 1.5 or afterward version. Apart from public, static together with void, in that location are certainly keywords similar final, synchronized together with strictfp which are permitted inwards the signature of coffee top dog method.

Why top dog method is static inwards Java

 is the get-go programming method a Java programmer knows when he starts learning Java prog Why top dog method is world static inwards JavaNow come upward to the top dog signal "Why the top dog method is static inwards Java", in that location are quite a few reasons some but hither are few reasons which brand feel to me:

1. Since the top dog method is static Java virtual Machine tin telephone vociferation upward it without creating whatever event of a cast which contains the top dog method.

2. Since C together with C++ likewise convey similar top dog method which serves every bit entry signal for plan execution, next that convention volition alone assistance Java.

3. If top dog method were non declared static than JVM has to practise event of top dog Class together with since constructor tin hold out overloaded together with tin convey arguments in that location would non hold out whatever certainly together with consistent way for JVM to uncovering top dog method inwards Java.

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

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

Why the top dog method is void inwards Java
Since the top dog method inwards Java is non supposed to render whatever value, it's made void which exactly agency top dog is non returning anything.

Summary:
1. The top dog method must hold out 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 uncovering the top dog method of predefined signature inwards cast which is provided to Java command. E.g. if you lot run coffee Helloworld than JVM volition search for world static void top dog String args[]) method inwards HelloWorld.class file.

3. The top dog method is an entry signal for whatever Core Java program. Execution starts from the top dog method.

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

5. When you lot encounter "Exception inwards Thread main” e.g.
Exception inwards Thread main: Java.lang.NullPointerException it agency Exception is thrown within top dog thread.

6. You tin declare the top dog 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 purpose a final, synchronized together with strictfp modifier inwards the signature of the top dog method inwards Java.

8. The top dog method inwards Java tin hold out overloaded similar whatever other method inwards Java but JVM volition alone telephone vociferation upward the top dog method amongst specified signature specified above.

9. You tin purpose throws clause inwards the signature of the top dog method together with tin throw whatever checked or unchecked Exception.

10. A static initializer block is executed fifty-fifty earlier JVM calls the top dog 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

Belum ada Komentar untuk "Why Principal Method Is World Static Inward Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel