public main? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

public main?

What will happen if I write the main method signature as public instead static? Like this: public void main(String[ ] args)

17th Jun 2017, 9:27 PM
Edson
Edson - avatar
7 Answers
+ 6
"static: method can be run without creating an instance of the class containing the main method".
17th Jun 2017, 9:20 PM
Edson
Edson - avatar
+ 4
It must be both public and static.
17th Jun 2017, 9:21 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
You'll get an error saying that the Main Method should be set to static
17th Jun 2017, 9:14 PM
Limitless
Limitless - avatar
+ 3
No, not really. But it'll show a message error?
17th Jun 2017, 9:16 PM
Edson
Edson - avatar
+ 1
Yes
17th Jun 2017, 9:16 PM
Limitless
Limitless - avatar
+ 1
Here is what they meant by that statement: https://code.sololearn.com/cYIUAWE6PLRE/?ref=app
17th Jun 2017, 9:29 PM
Limitless
Limitless - avatar
0
Be aware that I did not call upon: Animal dog = new Animal("Dog","Male","Woof"); I just used Animal.makeSound(); "static: method can be run without creating an instance of the class containing the main method". The makeSound method is set to static thus I can access it directly from the class without creating an instance
17th Jun 2017, 9:30 PM
Limitless
Limitless - avatar