What is wrong with the code below? Can anybody say why can't i call a non-static method from a static method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is wrong with the code below? Can anybody say why can't i call a non-static method from a static method?

class MyClass { void sayHello(String name) { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello("David"); sayHello("Amy"); } }

25th Jul 2018, 8:36 AM
Nathan
Nathan - avatar
7 Answers
+ 7
a non-static method of a class must have an instance of the class to process. MyClass obj = new MyClass(); obj.sayHello("David");
25th Jul 2018, 9:07 AM
John Wells
John Wells - avatar
+ 5
Rugved Modak Nathan I don't mind it at all.
25th Jul 2018, 2:32 PM
John Wells
John Wells - avatar
+ 3
Just so you know Nathan , John Wells is much elder and a lot more experienced than you, to call him mate.
25th Jul 2018, 2:20 PM
Rugved Modak
Rugved Modak - avatar
+ 3
Ok. I think it's just your modesty.
25th Jul 2018, 2:48 PM
Rugved Modak
Rugved Modak - avatar
+ 3
You could simply see his display pic and gold mod status. Anyways, if he doesn't have a problem, I won't mind too.
25th Jul 2018, 4:05 PM
Rugved Modak
Rugved Modak - avatar
+ 2
Thanks mate!
25th Jul 2018, 9:12 AM
Nathan
Nathan - avatar
+ 1
I was just focusing on the answer so i didn't see his profile 😅
25th Jul 2018, 3:53 PM
Nathan
Nathan - avatar