javadrjava

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


I'm trying to learn Java using DrJava.

When I try to run this bit of code DrJava tells me:

Static Error: This class does not have a static void main method accepting String[].

What I don't get is why DrJava tells me there's no main. It's the first line after the class canine declaration; and I don't see any typos or missing punctuation.

I tried Googling this, but I didn't understand what they were talking about.

Would be nice if someone can clue me in, in a way that doesn't actually give me the answer, but leads me figure it out on my own why this is happening to me -but if the problem is too basic to create a learning opportunity, then I'll take a solution; I guess.

/*
 * This is an exercise designed to practice creating a class Animal
 * And then creating another class canine in which to create an object dog.
 * The reason I want to call from one class to another is because I want 
 * to understand how classes, objects, inheritance, etc. works. 
 * Clearly, class canine is -in my mind at least, a child of class Animal.
 * The main method of canine then calls method attributes I think are being
 * inherited by dog and wolf,from the class Animal.    
 */

public class Animal {
  void growl() {
    System.out.println("Grrr");
  }
  void bark() {
    System.out.println("Arf! Arf!");
  }
}

class canine {
  public static void main(String[]args) {
    Animal dog = new Animal();
    dog.bark();

    Animal wolf = new Animal();
    wolf.growl();

  }
}

Solution

  • 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).