• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding package

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

Let's say in class A I have import java.text and class B I also import java.text in my code. Class A and Class B are in A.java and B.java respectively.

the mainAB class is in mainAB.java and has a main static make use of class A and B. How do I package the 3 classes together so tt I only require to import java.text once instead of 2 times in A.java and B.java.

Thanks alot
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a question of packaging the classes. The import statements are needed for compilation only, and each class is compiled separately, so each one needs to contain all the imports it utilizes.
Are you trying to save a few keystrokes for copying/pasting the import statements?
 
Norm Radder
Bartender
Posts: 1345
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of the import statement as an extension of the classpath for the compiler. It helps the compiler find classes you are using and keeps you from having to type the full package names when you use a class: java.util.Vector vec = ....

It sort of controls the namespace for classes.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Leong:
hi guys,

Let's say in class A I have import java.text and class B I also import java.text in my code. Class A and Class B are in A.java and B.java respectively.

the mainAB class is in mainAB.java and has a main static make use of class A and B. How do I package the 3 classes together so tt I only require to import java.text once instead of 2 times in A.java and B.java.

Thanks alot


The only way to make it so you only need one import statement is to put all three classes in the same file. However, I strongly suggest that you DON'T do this. Mostly because as the number of classes grow, a single file will be much, much too big for you to maintain. Just get used to the fact that every file needs to import the classes that it needs.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic