1

So I'm just starting to learn java and have come to this example:

class Dog {
    int size;
    String breed;
    String name;

    void bark() {
        System.out.println("Ruff! Ruff!");
    }
} // class Dog

class DogTestDrive {

        public static void main(String[] args) {
            Dog d = new Dog();
            d.size = 40;
            d.bark();
        } // end main
} // class DogTestDrive

When I try to run it I get the following error: Error: Main method not found in class Dog, please define the main method as: public static void main(String[] args)

I don't see where is the problem? This should work with only one main mathod.

3
  • 1
    You have to execute DogTestDrive. Commented Dec 26, 2013 at 0:04
  • 1
    java DogTestDrive - not java Dog. Commented Dec 26, 2013 at 0:07
  • Thank you all for help, it works now, i was a little confused since I'm trying to learn everything without ide, just notepad++ and cmd. This helped me understand things a little better. Commented Dec 26, 2013 at 10:48

5 Answers 5

3

When you start java, you tell it which class to execute. Java finds the main() method in the specified class and calls it.

You need to tell java to execute DogTestDrive.

Note that if you're executing this in an IDE it's as easy as right-clicking on DogTestDrive in your project and chiding "Run".


The class java runs must be a "top level" class - that is one that is declared in its own file of the same name as the class, but with .java added (not a class declared in another class's file).

Sign up to request clarification or add additional context in comments.

3 Comments

What if he has these in the same file, I believe the file has to be named DogTestDrive
@svenoaks good point. I didn't think of that... I'll make a note in the answer
Thank you for help, it helped me a lot. Considering this about same file, yes both of the classes are in the same file but filename doesn't have to be like the name of class it contains. I've tried to name file example.java and it compiles without problems. Also it makes 2 new files, for each class one, and when i run DogTestDrive it runs normal. I don't know if that's what you thought when you wrote "What if he has these in the same file".
1

Your main method is in the class DogTestDrive not the class Dog.

Comments

0

What Tim B means is that you have to run DogTestDrive instead of Dog. This way the main method from DogTestDrive is used and no main method in Dog is needed.

Comments

0

In Java, each file with a .java extension has one public class with the same name as the file (e.g. Dog.java has public class Dog {...). A file can have multiple classes, but only one can be public.

When you run a file named Dog.java, Java will try to execute Dog.main in the file. If you want DogTestDrive.main to run then you must put DogTestDrive in DogTestDrive.java As for class Dog {..., can either go in the same file or in its own Dog.java file, the latter being preferred because then classes outside of the file will be able to access it.

Comments

-1

Your code runs right, but make sure that the file is named "DogTestDrive.java", because you must run it. Further, you should declare the class DogTestDrive as public (not necessary).

  class Dog {
        int size;
        String breed;
        String name;

        void bark() {
            System.out.println("Ruff! Ruff!");
        }
    } // class Dog

    public class DogTestDrive {

            public static void main(String[] args) {
                Dog d = new Dog();
                d.size = 40;
                d.bark();
            } // end main
    } // class DogTestDrive

2 Comments

Having the class Dog being public and not in its own compile unit will cause a compilation failure. Furthermore, there is not requirement in this case for the class Dog to be public.
Omg you're right, I wanted to recommend the use of public when declare a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.