• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Duplicate class Error

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic