This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Package 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 » Java » Beginning Java
Bookmark "Package " Watch "Package " New topic


Bindu San

Joined: Mar 08, 2002
Posts: 5
I am writing a class "Date" in package "A".
And writing a class "Employee" in package "A"
(i am creating an object of DATE in class EMPLOYEE)
And then, i am inheriting the class "Employee" to a subclass "Boss" . Now, what classes are supposed to be placed in package "A" and does Boss needs an import statement ?
And, when we create a package, do we have to create a directory before hand ???
I appreciate some help .
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Bindu San:
Now, what classes are supposed to be placed in package "A" and does Boss needs an import statement ?

Well, you can place whichever classes you'd like inside package A. Packages are really designed to help you organize your classes. They can, however, be used to help set up accessibility to members.
The rule of thumb about imports is that, unless the class you're trying to get at is in the same package as the one the class that's trying to get at it is in, you'll need to designate the full name of the class. Take a look at this example:

The code above is illegal because the compiler can't find a class called Parent within package B (which is where class Child is). You have two options to resolve this - you can use an import statement or you can provide the fully qualified name of class Parent. If you replace the last line in the above example with either of these options, you'll be fine.

I hope this helps you understand how to deal with packages. If you have more questions, please ask.

SCJP Tipline, etc.
Bindu San

Joined: Mar 08, 2002
Posts: 5
Hi Corey ,
Thats a great help. But what i did is , i placed all my classes in the same package. If this is the case, will my import statement give any problem ???
Coz, i declared classed A and B in the same package. And B extends A. In the class B, i used an import statement,. Is this necessary ???
Still my program is in trouble...
Plz help me out
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
If I'm understanding you correctly...
No, you don't need to import the package that your class is already in. So:
package a;
import a.*; // unnecessary
class A {
Let me recommend Controlling Access To Members Of A Class as a good source of information straight from the horse's mouth.
Good Luck,
-Dirk Schreckmann

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Package
Similar Threads
classes inside an interface
struts2 iterator tag
Pet dogs of your boss in office
EJB-QL Question
Exception question for *REAL EJB MEN* (ok, Kathy, for REAL J2EE GIRLS too)