0
Why this program show output as "No Output" after removing static keyword?? please explain it detailed...
No output as a Output https://code.sololearn.com/coMnmhCq1dAh/?ref=app
6 Respostas
- 1
Yogeshwaran a static member belongs to the class and not to specific instance of the class. The main() must be 'static' because your program starts executing from the main() and so that it can be called without creating an object of the class.
But if you remove 'static' then the JVM need to create an object of the class to call the main() but this is not possible for the JVM to do so because creating an object means calling the constructor of the class and you can never be sure what type of constructor you exactly need to call and an ambiguity is raised. So this leads to a runtime error.
+ 1
In simple words, adding static keyword before variables and methods converts them into class variables and methods.
Now we can access them directly without creating objects using class.
0
Avinesh please tell me what is the use of static keyword in this code
0
Thanks Avinesh and Viren Varma for your guidance now I understood..
0
use static for main()
public static void main(String[ ] args) {
without it there is no entry point to run code
- 1
Do not touch the main method declaration.
This is an entry point for the JVM to begin code execution. Now since the method is not static, the JVM needs an instance of the class to access this method. It will lead to a runtime error.