0
Why main method cannot be instantiated in JAVA ?
â. The " MAIN METHOD" cannot be instantiated because it is declared as " STATIC " . â Main() is declared as STATIC because it is accessed by JVM . â Since it is the point where execution starts so the class loader is not able to have any point where an object is created . That's why main() is declared as STATIC & main() method cannot be instantiated . thank you ! hope it'll help u đ
5 RĂ©ponses
+ 8
thanks for sharing đ đ
+ 8
Please note Classes are instantiated not methods so it is not correct to say that the main method cannot be instantiated as methods are NEVER instantiated.
A static method is a method in a class that can be called without creating an object of that class, that is without instantiating the class
+ 3
Hi @Shubham Chaurasiya... There is a semantic issue, as well as, some technical oversimplifications in your explanation.
The semantic issue was accurately pointed out by @Yerucham. It might help to understand the word "instantiation" refers to the creation of an object "instance". Object "instances" are defined by class types. Therefore, classes get "instantiated" and methods can be defined as either "instance" or static members.
The following are different ways of saying what I think you meant to ask:
- Why main method cannot be an instance method in Java?
or
- Why main method must be a static method in Java?
---------------
As you can see, your 1st bullet, as written below, would not semantically make sense:
> â. The " MAIN METHOD" cannot be instantiated because it is declared as " STATIC ".
---------------
Regarding the 2nd and 3rd bullet points, as written below:
> â Main() is declared as STATIC because it is accessed by JVM
> â Since it is the point where execution starts so the class loader is not able to have any point where an object is created.
These last 2 bullet points imply the JVM cannot start from an instance method via an instantiated object. Technically speaking, a custom java launcher could be created to accept a few additional arguments to override the default application entry point specifying an instance method of a class with a different method name.
The JDK java launcher ( /usr/bin/java ) is actually written in C - which may be surprising to newer Java programmers. The java launcher uses the JNI (Java Native Interface) to explicitly set the main() entry point:
mainID = (*env)->GetStaticMethodID(env, mainClass, "main", ... );
Essentially, this JNI method returns the ID for a static method to invoke by the name of "main". Alternatively, this could have been:
mainID = (*env)->GetMethodID(env, loaderClass, loaderMethod, ... );
...where the loaderMethod is string variable containing the name of an instance method of the specified class.
+ 3
So... perhaps, the next question might be... why does Java require the application entry point to be a static public void method named "main" rather than using an instance method?
This can only be answered with speculation.
Although it's technically possible to use an instance method as the entry point, doing so would violate the JSL (Java Specification Language) which states in:
------------------
5.2. Java Virtual Machine Startup
... "The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution."
-See: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.2
------------------
It has been speculated by people in other online communities that it's just simpler to make the entry point static. To support an instance method, the following would be required:
- A default class constructor would need to be defined.
- The class cannot be abstract.
These are not required for static methods.
Hopefully, this alternative explanation is helpful.
0
static itself means that the copy is shared to all the objects and there is no need to create an object . static keyword is used for memory management .