12. Inheritance, extends (71%), code error?

the second part of the extends exercise:
“Within the main method of the Dog class, call the checkStatus method on the spike object.”

it is not ok to add the " spike.checkStatus();" at the beginning of main, like this:

public static void main(String[] args) {
Dog spike = new Dog(5);
int spikeAge = spike.getAge();

But it is ok if it’s typed below the last line (below println). Why?

Here is what you have shown us:

Dog spike = new Dog(5);

You are asking to check spike’s status but you haven’t even created spike yet.

1 Like

spike is an instance of Dog class. You have to create the spike instance first before you can use the new object spike with the classes methods.
You don’t have to use it after the println statement you have in your code. You can use it anywhere after your object declaration.

So it goes in a logical order.
I thought it loaded and compiled the whole class at the same time.

A post was split to a new topic: This is still wrong and I can’t go to next course