user3264667
user3264667

Reputation: 21

The public type Dog must be defined in its own file

For some reason I keep getting an error saying

The public type Dog must be defined in its own file

when I try to compile. Does anyone here know how I can fix it?. Anyone know what I did wrong? I tried looking at some other answers but it said some weird mumbo jumbo about importing something.

Upvotes: 0

Views: 8687

Answers (3)

Scary Wombat
Scary Wombat

Reputation: 44834

Move the code for the Dog class to a file called Dog.java All top-level classes e.g. the public Dog class you want to call from another class must exist in their own specially named files.

Upvotes: 1

Each Java compilation unit (a .java file in all common systems) must contain exactly one top-level type (class, enum, or interface) whose simple name exactly matches the name of the file without the .java extension.

It doesn't matter what the access modifier on the class is (public or default), and public nested classes can be placed inside the top-level type, but the single top-level type must match the file name.

(This requirement is technically optional at the discretion of the compiler implementer, but I don't know of any compilers that don't enforce it, and code should always assume the compiler will.)

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13844

If your file name and public class name differs then you get this type of error.

From the error The public type Dog must be defined in its own file I am pretty sure that your file name is not Dog.java

Upvotes: 3

Related Questions