0

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.*;
5
  • It is always good to put all your classes in packages. How you name them it all depends on you. Only thing you want to consider is to name them according to Java Naming Convention. If I ware you I would probably also make MyIterator inner class of LinkedList and make it implement standard Iterator class.
    – Pshemo
    Feb 23, 2014 at 9:54
  • Anyway this question seems to be primarily opinion based, unless you are facing some problems with your current organisation of packages. If it is so then please post informations about errors you are seeing.
    – Pshemo
    Feb 23, 2014 at 9:58
  • Yes, I am facing problems. Can you give me an example of naming them ?
    – rnbguy
    Feb 23, 2014 at 10:00
  • Your classes can be organised in lot of correct ways and even more not correct ones. Please describe your problem you are facing so we could help you better. Do you see some errors while importing? Which access modifiers have your classes? Update your question with more details.
    – Pshemo
    Feb 23, 2014 at 10:04
  • updated OP, please check.
    – rnbguy
    Feb 23, 2014 at 10:05

1 Answer 1

1

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
3
  • You are welcome. Next time don't forget to describe problem you are facing beside describing what you want to achieve :)
    – Pshemo
    Feb 23, 2014 at 10:12
  • 1
    Okay. But one last question, I already had import MyProject.MyExeceptions.* in MyIterator, then why I needed to import it again in LinkedList ?
    – rnbguy
    Feb 23, 2014 at 10:16
  • 1
    Imports are only used in class in which they are placed. If you want to use Exceptions class from MyProject.MyExceptions in other classes you need to include them there as well.
    – Pshemo
    Feb 23, 2014 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.