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
java
command-line tool:java canine
When you do
java canine
, you'll telling thejava
tool to find and load thecanine
class and run itsmain
method.If you were using
java Animal
, the issue is thatAnimal
has nomain
.canine
does.No, there is no relationship between
canine
andAnimal
other than thatcanine
usesAnimal
in itsmain
. E.g.,canine
depends onAnimal
but isn't otherwise related to it. If you wanted it to be a subclass (one fairly reasonable interpretation of "child class"), you'd addextends Animal
to 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
canine
in the same file asAnimal
, makingAnimal
public, and then expecting DrJava to figure out that it should runcanine.main
rather 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
Canine
rather thancanine
.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 withcanine
inAnimal.java
), but in general, again, best practice is to separate them. So you'd haveAnimal.java
containing theAnimal
class, andcanine.java
containing thecanine
class (or better,Canine.java
containing theCanine
class).