Why this program show output as "No Output" after removing static keyword?? please explain it detailed... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

13th May 2020, 2:27 PM
Yogeshwaran
6 Answers
- 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.
13th May 2020, 2:45 PM
Avinesh
Avinesh - avatar
+ 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.
13th May 2020, 2:41 PM
Viren Varma
Viren Varma - avatar
0
Avinesh please tell me what is the use of static keyword in this code
13th May 2020, 2:37 PM
Yogeshwaran
0
Thanks Avinesh and Viren Varma for your guidance now I understood..
13th May 2020, 2:49 PM
Yogeshwaran
0
use static for main() public static void main(String[ ] args) { without it there is no entry point to run code
13th May 2020, 5:20 PM
zemiak
- 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.
13th May 2020, 2:32 PM
Avinesh
Avinesh - avatar