+ 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
3 Antworten
+ 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();
}
}
+ 1
ohhh!!
thank you!!
0
you did not close Animal class before main class, but close it at the end of code