+ 3

Explain this program

class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } }

18th Mar 2019, 12:37 AM
Priyanka Gotugade.
Priyanka Gotugade. - avatar
3 Respuestas
+ 6
"sayHello()" in main calls "static void sayHello()" outside of main, this method returns nothing and just executes what ever is inside the methods body.
18th Mar 2019, 1:55 AM
D_Stark
D_Stark - avatar
+ 2
the my class is an OBJECT {}which contains two functions(){}. a say hello func, and a main func. the say hello func does just that, but it is dormant untill called. the main func is special, as it is responsible for calling other funcs. in this case it calls the say hello func.
18th Mar 2019, 1:39 AM
Logomonic Learning
Logomonic Learning - avatar
0
Your method sayHello() are static it means it belongs to the class MyClass so you no need to create an objekt. Lets say your method not is static, then we will have to create an object in your main method: MyClass m = new MyClass(); Now use the reference variable (m) to call your method m.sayHello();
18th Mar 2019, 1:52 AM
JavaBobbo
JavaBobbo - avatar