+ 1

Why do we need to declare a method with static and void?

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

2nd Jan 2017, 5:57 AM
Mohd Kaleem Khan
Mohd Kaleem Khan - avatar
1 Answer
+ 2
We declare methods static so they can be accessed without holding an instance of an Object based on that class. And We declare methods void as the method is not returning any value.
2nd Jan 2017, 5:59 AM
Mohd Kaleem Khan
Mohd Kaleem Khan - avatar