+ 3
Explain this program
class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } }
3 Answers
+ 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.
+ 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.
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();