Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jesse Black   on May 14 In Java Category.

  
Question Answered By: Randy Warren   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?

Share: 

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


Tagged: