Pregunta

So I have these classes,

Exception1.java, Exception2.java, LinkedList.java, Iterator.java

LinkedList needs Iterator, Iterator needs exceptions and exceptions are independent.

So I put them in folder like this -

                     MyProject
       __________________|____________________
      /                  |                    \
LinkedList.java     MyIterator.java      MyExceptions
                                    ___________|__________
                                   /                      \
                            Exception1.java         Exception2.java

Now how should I name the packages and import them in between them, such that, I can make a test.java outside of MyProject and use LinkedList class?

Answer :

I linked them like this.

LinkedList : package MyProject; import MyProject.MyExceptions.*;
MyIterator : package MyProject; import MyProject.MyExceptions.*;
Exception1 : package MyProject.MyExceptions;
Exception2 : package MyProject.MyExceptions;
test : import MyProject.*; import MyProject.MyExceptions.*;
¿Fue útil?

Solución

Your problem is that import your.package.* works only for classes in your.package. It means that it will not import classes from its subpackages like your.package.exceptions. To import them you need to create separate import. Try with

import MyProject.*;//this can import LinkedList and MyIterator
import MyProject.MyExceptions.*;// this can import Exception1 and Exception2

//rest of your code
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top