• 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

Accessing a package from another file

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the code above is stored as Animal.java




the code above is stored as Main.java.

but when i compile Main.java, i get an error saying that Animal is not accessible. They are both stored in the same directory.

Please tell what my mistake is.
 
Saloon Keeper
Posts: 15528
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your mistake is that they're in the same directory.

Animal needs to be in directory G, which is a subdirectory of the directory Main is in.
 
sinatra roger
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the issue was solved but all the 3 files below are in the same directory inside the same project..so i dont understand what the post above actually says..

code below is stored in Animals.java .



code below is stored in Elephant.java .



code below is stored in Main.java .

 
Stephan van Hulst
Saloon Keeper
Posts: 15528
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, classes need to be in a directory structure that reflects the package structure.

If you have a class a.b.c.Foo, then the file must be saved as a/b/c/Foo.java, relative to your project source folder.

So your classes Elephant and Animals need to be in files G/Animals.java and G/Elephant.java, while Main.java needs to be in the top directory.
 
sinatra roger
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Again, classes need to be in a directory structure that reflects the package structure.


So your classes Elephant and Animals need to be in files G/Animals.java and G/Elephant.java, while Main.java needs to be in the top directory.



thanks for the advice..i will make it a practice.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic