Having trouble creating a new object from a class in Eclipse
Eclipse says there's an error with the keyword "new" and "dog" in the following code, but I've copied this example straight off a book. I don't know what's wrong here
Eclipse error #1:Dog cannot be resolved to a variable error #2:syntax error on token "new", delete this token
package pkg;
// creating the Dog class
class Dog {
int size;
String breed;
String name;
void bark(){
System.out.println("Ruff! Ruff!");
}
}
// this function does the testDrive
public cl开发者_开发百科ass HelloWorld {
public static void main(String[] args) {
// problem occurs here, both "new" and "dog" underlined
Dog d = new Dog;
d.size = 40;
d.bark();
}
}
You are missing the constructor braces, more specifically:
Dog d = new Dog();
You need to say new Dog()
, not just new Dog
.
Alas, Java is not C++; you don't get to omit the brackets just because you're using the default constructor.
精彩评论