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();
}
}
That code runs just fine with the
javacommand-line tool:java canineWhen you do
java canine, you'll telling thejavatool to find and load thecanineclass and run itsmainmethod.If you were using
java Animal, the issue is thatAnimalhas nomain.caninedoes.No, there is no relationship between
canineandAnimalother than thatcanineusesAnimalin itsmain. E.g.,caninedepends onAnimalbut isn't otherwise related to it. If you wanted it to be a subclass (one fairly reasonable interpretation of "child class"), you'd addextends Animalto its declaration. If you wanted it to be a nested class (another fairly reasonable interpretation of "child class"), you'd put it insideAnimal.From your comment:
I expect you're confusing DrJava by putting
caninein the same file asAnimal, makingAnimalpublic, and then expecting DrJava to figure out that it should runcanine.mainrather thanAnimal.main. See the notes below about best practices.From another comment:
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
Caninerather thancanine.Side note 2: As Hovercraft Full Of Eels says, it's best to put each class in its own
.javafile, named by the name of the class. Technically, you can put non-public classes in any.javafile (which is why that code works withcanineinAnimal.java), but in general, again, best practice is to separate them. So you'd haveAnimal.javacontaining theAnimalclass, andcanine.javacontaining thecanineclass (or better,Canine.javacontaining theCanineclass).