• Post Reply Bookmark Topic Watch Topic
  • New Topic

Duplicate class Error  RSS feed

 
Shelly Biswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java class called TestHippo.
While compiling i get the following error:
D:\shell\Personal\Tech\Java\TestHippo.java:1: duplicate class: Animal
class Animal {
^
1 error


Following is the code:

 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you don't yet have another class called Animal in the same package? Maybe in it's own file Animal.java , or just like TestHippo here in another file?
When that has already been compiled and is in the path, the compiler will give this type of error.
 
Shelly Biswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there was another class named Animal in another java class called o.java.
i dnt understnd why that should affect my class animal in another java class.
cant i ve same class names in two different java files???
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shelly Biswal:
cant i ve same class names in two different java files???

Not if they are in the same package as well.

Imagine the situation where you have two (non-public) classes in two different files with the same fully qualified name (so same package etc). Then you want to use that class. But which one?
That's why I put all classes of my (real, non-test) code in separate files, so this problem can never arise.

Please note that you can still have the same name if they are in different packages, like java.awt.List and java.util.List, or if they are in the any package but at least one of them is a nested class like javax.swing.text.html.Parser and javax.swing.text.html.HTMLEditorKit.Parser. The first is a top level class, the second is a nested class inside top level class HTMLEditorKit.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!