Why main method is static and public in java
Why is main method declared static in Java? Why is main method public in Java? Rate this post. We are sorry that this post was not useful for you! Tell us how we can improve this post?
Submit Feedback. In this case, main must be declared as public , since it must be called by code outside of its class when the program is started. The keyword static allows main to be called without having to instantiate a particular instance of the class.
This is necessary since main is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main does not return a value. The true entry point to any application is a static method. If the Java language supported an instance method as the "entry point", then the runtime would need implement it internally as a static method which constructed an instance of the object followed by calling the instance method.
With that out of the way, I'll examine the rationale for choosing a specific one of the following three options:. Keep in mind that one of the design goals of Java was to emphasize require when possible good object-oriented programming practices.
In this context, the constructor of an object initializes the object, but should not be responsible for the object's behavior. Therefore, a specification that gave an entry point of new ClassName would confuse the situation for new Java developers by forcing an exception to the design of an "ideal" constructor on every application. By making main an instance method, the above problem is certainly solved. However, it creates complexity by requiring the specification to list the signature of the entry class's constructor as well as the signature of the main method.
In summary, specifying a static void main creates a specification with the least complexity while adhering to the principle of placing behavior into methods.
Considering how straightforward it is to implement a main method which itself constructs an instance of a class and calls an instance method, there is no real advantage to specifying main as an instance method.
I don't know if the JVM calls the main method before the objects are instantiated But there is a far more powerful reason why the main method is static When JVM calls the main method of the class say, Person.
You see, the JVM invokes it by the class name. That is why the main method is supposed to be static and public so that it can be accessed by the JVM. The static key word in the main method is used because there isn't any instantiation that take place in the main method. But object is constructed rather than invocation as a result we use the static key word in the main method.
In jvm context memory is created when class loads into it. And all static members are present in that memory. The method must be declared public and static , it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked.
A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header. The public static void keywords mean the Java virtual machine JVM interpreter can call the program's main method to start the program public without creating an instance of the class static , and the program does not return data to the Java VM interpreter void when it ends.
And in case of main method, we are making it as an STATIC because it is nothing to do with object, as the main method always run whether we are creating an object or not. Any method declared as static in Java belongs to the class itself. So the main method is declared as static so that it can be accessed without creating an object of that class. Since we save the program with the name of the class where the main method is present or from where the program should begin its execution, applicable for classes without a main method Advanced Level.
So by the above mentioned way:. In brief when the program is compiled it searches for the main method having String arguments like: main String args[] in the class mentioned i. From java. The main method is static to give the Java VM interpreter a way to start the class without creating an instance of the control class first. Instances of the control class are created in the main method after the program starts. My understanding has always been simply that the main method, like any static method, can be called without creating an instance of the associated class, allowing it to run before anything else in the program.
If it weren't static, you would have to instantiate an object before calling it-- which creates a 'chicken and egg' problem, since the main method is generally what you use to instantiate objects at the beginning of the program. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why is the Java main method static? Ask Question. Asked 13 years, 1 month ago. Active 1 year, 1 month ago.
Viewed k times. Java is deliberately designed to look familiar to a C programmer. This is very close to the C convention. Add a comment. Active Oldest Votes. Interesting, I do disagree with the "This is just convention. The OP's primary question was the reason for static in the declaration.
I don't think static in the main declaration is just for the sake of convention. David So it did. I actually would have preferred an answer from one of the people originally involved — but that was a very far shot. Most of the other answers are unfortunately an exercise in ad-hoc reasoning. This one gives quite interesting details, besides having the humility not to invent wrong technical details to reason away a probably non-technical cause. Jared - They could have required a public no-arg constructor and made main non-static and still fit within the bounds of the language.
Without hearing from the designers, we'll just have to agree to disagree. BenVoigt You call LoadLibrary to get the jvm dll. Once the VM is loaded you use standard JNI calls to find the correct class, load the static main method and invoke it.
There's not a lot of room for misinterpretation there. JNI is absolutely how you load the VM. You may be used to writing only client side JNI using the native keyword, javah -jni, etc Show 12 more comments.
What should it pass for x? I have no idea why main is always marked public though. Implementing an interface does not solve the instantiation problem. I personally like that public static void main serves as a marker of an entry point — a public parameterless constructor doesn't scream out "This is probably an entry point! EdwinDalorzo - What would be gained by forcing the entry point class to be instantiated?
Calling a static method places the least amount of burden on the class. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology.
Cyber Security. C Programming. Control System. Try to avoid saying things like your idea fails. Made minor changes. Changed the first letter of the class to uppercase, but you might not have to. Sir I want to implement push notifications like your site. My website is built on spring and jsp. Please help me out of this.
Please look into their documentation for implementing it. You can also look at some other services for push notification. Your email address will not be published.
Prev Java Anonymous Class. Next json-simple example. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments Jackson says:. October 18, at am. Ackeem says:. April 7, at pm. Shubham Ray says:. September 13, at pm. Dia says:. October 28, at pm. December 28, at am. August 26, at pm.
April 28, at am. Haribabu says:. July 5, at am. Ramya says:. December 10, at am. Ali says:. February 2, at pm. Fareha says:. March 11, at am. August 11, at am. August 21, at pm. Ricardo says:. February 6, at pm.
December 11, at pm.
0コメント