The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "packages" Watch "packages" New topic


Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Hi everyone,
I can't seem to get my package members to recognize each other. I have to files as follows:


The files are both saved in a directory called \java\interfacetest. Both \java and \interfacetest are included in the classpath. I only have one j2sdk installed too. I'm just doing some testing so I can understand casting to interfaces for the scjp, but I can't get it to compile! Here are the errors:
C:\java\interfacetest\tree.java:2: package interfacetest does not exist
import interfacetest.branch;
C:\java\interfacetest\tree.java:4: cannot resolve symbol
symbol : class branch
location: class java.interfacetest.tree
public class tree implements branch
C:\java\interfacetest\tree.java:6: cannot resolve symbol
symbol : class branch
location: class java.interfacetest.tree
public static branch b;
3 errors
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

try putting your packages in a bit more logical order. Right now, I think you structure looks like this:

try reorganizing things like this (it's too confusing to have two packages names the same thing in different locations):

2nd thing, you might want to rethink your architecture (Aggregation vs Inheritance).
Basically the "has-a" vs the "is-a" relationship. A tree has-a branch (aggregation). You can't say that: A tree is-a branch (inheritance) so it doesn't really make sense implement the branch interface.
What if you did something like this?
interface tree
class deciduous implements tree
class evergreen implements tree
class oak extends deciduous
class pine extends evergreen
class spruce extends evergreen
then those classes could contain objects of type branch, type leaf or type needle etc.
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Yeah, I didn't really plan out the names. My goal was to test if an interface took on the members of a class by declaring an interface and then assigning it to a class. What I meant to set up was
package interfacetest
/ \
/ \
tree(class) branch(interface)
and have the tree implement the branch. But it can't find it. Am i doing something wrong with the package declaration? Good point with the sloppy naming though.
Alan Phillips
Ranch Hand

Joined: Aug 13, 2002
Posts: 73
Sorry, the above didn't come out the way I wanted. I want it to look exactly like your second example!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You do not need the "java." in the package statement in the second code snippet. BTW you should not use "java", "javax", and a host of other prefixes, because they are considered reserved by Sun.
If your two files have the same package statement, here "package interfacetest", then you do not need the import statements.
Be aware that there are requirements on the names of the directories in which the files reside and your classpath must be defined correctly, so to find theses directories.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
It is sorta covered in the JavaRanch Style Guide.
subject: packages
Similar Threads
How can I get my HelloWorld Servlet to Run?
interfaces used to work on my system but now they dont :)
import and package compile errors
Cannot Resolve Symbol Error!
an unbelievable question about import!!!