Why can't I add static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why can't I add static?

My code: public class Animal { void bark() { System.out.println("Woof-Woof"); } void meow() { System.out.println("Meow"); } class MyClass { public static void main(String[ ] args) { Animal dog = new Animal(); Animal cat = new Animal(); dog.bark(); cat.meow(); } } } Problem: DrJava-- Error Message: The method main cannot be declared static; static methods can only be declared in a static or top level type BlueJ-- Error Message: Illegal static declaration in inner class Animal.MyClass modifier 'static' is only allowed in constant variable declorations

4th Sep 2018, 1:40 PM
Stephen Van Der Westhuizen
Stephen Van Der Westhuizen - avatar
3 Answers
+ 4
You can not declare it in the inner class. public class Animal { void bark() { System.out.println("Woof-Woof"); } void meow() { System.out.println("Meow"); } public static void main(String[ ] args) { Animal dog = new Animal(); Animal cat = new Animal(); dog.bark(); cat.meow(); } }
4th Sep 2018, 1:46 PM
Steppenwolf
Steppenwolf - avatar
+ 1
ohhh!! thank you!!
4th Sep 2018, 2:05 PM
Stephen Van Der Westhuizen
Stephen Van Der Westhuizen - avatar
0
you did not close Animal class before main class, but close it at the end of code
5th Sep 2018, 9:07 PM
Oleksiy Fedorchenko📱🇺🇦
Oleksiy Fedorchenko📱🇺🇦 - avatar