Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

the imports command

  Asked By: Jesse    Date: May 14    Category: Java    Views: 740
  

can somebody tell me how to create a java package.

I have defined a class called RQmsg in a file called RQmsg.java

I want to use this class in a file InRQ.java

How do I go about doing this. I am not sure how to use the import
command in this case

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Randy Warren     Answered On: May 14

to create  a java  package, simply add "package ~PACKAGENAME~;" at the
top of each class  you want in the package. When compiled, make sure
the .class files are all in a folder of the same name as the package.
package  can contain sub-packages.

Any class in a package can access public and protected members/methods
of any class in the same package without any importing.

To make public and protected members/methods of a class available to a
class in a different package, add "import ~.PACKAGE~.~CLASSNAME~;" to
the class.

Example:
Assume I have the following classes and packages:

Package1
-A
-B
-C

Package2
-foo
-bar

Any public and protected members/methods in A can be accessed by B and
C. Likewise, any in foo can be accessed by bar. However, anything in
Package1 cannot be accessed by anything in Package2.

To allow access to A from foo, add "Package1.A;" to foo.java

If you want to allow access to all classes in Package1, use "Package1.*;"

Any other questions?

 
Didn't find what you were looking for? Find more on the imports command Or get search suggestion and latest updates.




Tagged: