package and import efficiency

S

Sal

I have a directory that contains many classes in the same package and
a program that uses just one of those classes. Is there any advantage
to moving that program outside the directory and import only the class
it needs over keeping it in the directory and making it part of the
same package, eliminating the need for the import?
 
A

Arne Vajhøj

I have a directory that contains many classes in the same package and
a program that uses just one of those classes. Is there any advantage
to moving that program outside the directory and import only the class
it needs over keeping it in the directory and making it part of the
same package, eliminating the need for the import?

I am not quite sure that I understand the question.

But anyway:
* directory structure and package structure should match
* package structure should reflect the logical structure
in your software
* full name vs import vs same package is only relevant at compile
time - at runtime all classes are referenced using full names

Arne
 
L

Lew

Sal said:
I have a directory that contains many classes in the same package and
a program that uses just one of those classes. Is there any advantage
to moving that program outside the directory and import only the class
it needs over keeping it in the directory and making it part of the
same package, eliminating the need for the import?

No.

There isn't any need for the import in the first place.

Imports are compile-time occurrences only. Class load takes the same time
regardless, and is on demand. If a class is used, it's loaded at runtime.

If the "just one" class uses any of the other classes in that package, they'll
get loaded on demand, too.

Classes don't have directories, they have packages. Class *files* are stored
in directories, though not necessarily.
 
M

Martin Gregorie

I have a directory that contains many classes in the same package and a
program that uses just one of those classes. Is there any advantage to
moving that program outside the directory and import only the class it
needs over keeping it in the directory and making it part of the same
package, eliminating the need for the import?

I'd say its fine the way it is provided that the classes are only used by
one or more programs that are all part of the same package. If the list
of classes is large there may be a case for leaving the program(s) where
they are and making the classes into an inner package but I'd only do
that if it will make maintenance, etc. easier. Of course, if other
programs are likely to use the same classes, then there's a clear case
for moving the program somewhere else so the package is purely a class
library.
 
R

Roedy Green

I have a directory that contains many classes in the same package and
a program that uses just one of those classes. Is there any advantage
to moving that program outside the directory and import only the class
it needs over keeping it in the directory and making it part of the
same package, eliminating the need for the import?

If you just want to use the class as is, there is no advantage. If
you moved it, you could access the default scope methods and
variables.

However, cloning a class is generally a very very bad thing to do. You
will update one and forget to update the other.
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is no harm in being sometimes wrong especially if one is promptly found out.
~ John Maynard Keynes (born: 1883-06-05 died: 1946-04-21 at age: 62)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top