Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages

 
Bob Smithsoon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file called Class1.java and inside I have 3 classes defined, one is public access the other two are package access, I have another file called class2.java, how can I access the package access classes in class1 within class2? I tried putting class1 into a package and then importing that package in class2, but its not working, thanks.
 
Norm Radder
Bartender
Posts: 1361
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If "not working" means an error message, post it.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Welcome to JavaRanch!

In general, the idea is that if you have multiple classes in a source file, some of them won't have the same name as the source file, and so they're a little hard for the compiler to find. Therefore, the best plan is to only include multiple classes in a single file if those classes won't be used by other classes in other files, but only by classes in the same file. Otherwise, it just makes life easier for each class to be in its own file, with the file named after the class.

But the technical solution is to compile the multiple source files on the same command line, at the same time; in other words, whereas

javac class2.java

might fail,

javac class1.java class2.java

should work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic