开发者

Having trouble creating a new object from a class in Eclipse

开发者 https://www.devze.com 2023-04-01 22:39 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消