简体   繁体   中英

Java main class not found(error free)

here is my source code i named my Project as Animal but when I run it. There is an error locating my MAIN CLASS. Please help me. I am still a beginner and i am open to learn a lot more.

package animal;

public class Animal {  
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

public class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A bird sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A bird eats...");
    }
}

public class Dog extends Animal {
    public Dog() {
        super();
        System.out.println("A new dog has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A dog sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A dog eats...");
    }
}

public class MainClass {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Bird bird = new Bird();
        Dog dog = new Dog();
        System.out.println();

        animal.sleep();
        animal.eat();
        bird.sleep();
        bird.eat();
        dog.sleep();
        dog.eat();
    }
}

Please help me with this.

Ideally, you should have Bird, Dog, and Animal in their own files on the same directory. Then you create one more class, a tester class, that has a main function, and since it's in the same directory as Bird, Dog, and Animal, it can access them and you can do what your main class does now. If you NEED to have it all in one file, make every class header just class Bird { , etc. Then rename the file to MainClass.java and it'll run.

Please refer: Can a java file have more than one class?

For short: there can only be one public top-level class per .java file.

So, you should edit your code:

package Animal;

class Animal {
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

and so on ...

As studied your code,found the error only on class modifier(public) you declared.

" you can have only one public class modifier in a java source file"

We can declare one class in a single source file with these constraints:

  1. Each source file should contain only one public class and the name of that public class should be similar to the name of the source file.
  2. If you are declaring a main method in your source file then main should lie in that public class.
  3. If there is no public class in the source file then main method can lie in any class and we can give any name to the source file.

So.. Your code is..

   class Animal {  
        public Animal() {
    System.out.println("A new animal has been created!");
   }
   public void sleep() { 
    System.out.println("An animal sleeps...");
  }
  public void eat() {
    System.out.println("An animal eats..."); }
  }
  class Bird extends Animal {
   public Bird() {
    super();
    System.out.println("A new bird has been created!");
   }
   @Override
   public void sleep() {
    System.out.println("A bird sleeps...");
   }
    @Override
    public void eat() {
    System.out.println("A bird eats...");
    }
    }
    class Dog extends Animal {
     public Dog() {
    super();
    System.out.println("A new dog has been created!");
    }
     @Override
     public void sleep() {
    System.out.println("A dog sleeps...");
    }
     @Override
     public void eat() {
     System.out.println("A dog eats...");
       }
      }
     public class MainClass {
     public static void main(String[] args) {
      Animal animal = new Animal();
      Bird bird = new Bird();
      Dog dog = new Dog();
      System.out.println();

    animal.sleep();
    animal.eat();
    bird.sleep();
    bird.eat();
    dog.sleep();
    dog.eat();}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM