[Solved] DrJava tells me there’s no main, but main is defined within the class declaration


That code runs just fine with the java command-line tool: java canine

When you do java canine, you’ll telling the java tool to find and load the canine class and run its main method.

If you were using java Animal, the issue is that Animal has no main. canine does.

Clearly, class canine is -in my mind at least, a child of class Animal

No, there is no relationship between canine and Animal other than that canine uses Animal in its main. E.g., canine depends on Animal but isn’t otherwise related to it. If you wanted it to be a subclass (one fairly reasonable interpretation of “child class”), you’d add extends Animal to its declaration. If you wanted it to be a nested class (another fairly reasonable interpretation of “child class”), you’d put it inside Animal.

From your comment:

But I still don’t understand why DrJava is telling me that it doesn’t have a static void main method accepting String[]. Also, It’s not printing anything, when I run on DrJava.

I expect you’re confusing DrJava by putting canine in the same file as Animal, making Animal public, and then expecting DrJava to figure out that it should run canine.main rather than Animal.main. See the notes below about best practices.

From another comment:

but doesn’t dog.bark() directly call the function in Animal? Why do I need to “extend” in this scenario?

You don’t. A class can use another class without there being any inheritance relationship between them, as your code does. It was your use of the term “child class” in the comments that suggested you’d intended inheritance or similar.


Side note: While you don’t have to follow them, following standard Java naming conventions is good practice. Class names should be initially-capped and CamelCase. So Canine rather than canine.

Side note 2: As Hovercraft Full Of Eels says, it’s best to put each class in its own .java file, named by the name of the class. Technically, you can put non-public classes in any .java file (which is why that code works with canine in Animal.java), but in general, again, best practice is to separate them. So you’d have Animal.java containing the Animal class, and canine.java containing the canine class (or better, Canine.java containing the Canine class).

3

solved DrJava tells me there’s no main, but main is defined within the class declaration