Packages in Java; package and import
Java language conventions recommend using packages for grouping and organization of classes, interfaces, and annotations. These divisions are called namespaces.
Naming Java packages
The idea is to create packages with unique names to avoid name collisions. For packages prefix, it is recommended the use of internet domains as "edu4java.com" written the other way round;"com.edu4java".
For a game called killthemall, the names of the packages might be:
com.edu4java.killthemall.game com.edu4java.killthemall.ui com.edu4java.killthemall.worldlogic
Java packages naming convention
There is no clear official definition, but a de facto convention:
-
Packages must be lowercase identifiers, separated by a dot.
-
Do not use any other symbol beyond "." to separate words. E.g: killthemall, worldlogic
“Package” keyword
Most java source code files, begin with the keyword; package. When adding a package to our HelloWorld class it looks like this:
package com.edu4java.javatutorials; import javax.swing.JOptionPane; public class HelloWorld { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello World"); } }
From now on, the full name of our class is:
com.edu4java.javatutorials.HelloWorld
We can call this class from any other class of another package. Example:
package com.edu4java.javatutorials.otherpackage; public class CallHelloWorld { public static void main(String[] args) { com.edu4java.javatutorials.HelloWorld.main(null); } }
“Import” keyword
A very convenient option when using a class or interface from another package, is the use of the keyword import. If we use import, it is not necessary to write the full name of the class with the package included. It's enough with the class name.
import com.edu4java.javatutorials.HelloWorld; public class CallHelloWorld { public static void main(String[] args) { HelloWorld.main(null); } }
It is interesting to know that the classes in java.lang package are automatically imported. If we for example use the String class, you don´t need to import it. We can see below an example of this; we use java.lang.String to declare s1, and only String s2 to declare s2 and they both work.
package com.edu4java.javatutorials.otherpackage; import com.edu4java.javatutorials.HelloWorld; public class CallHelloWorld { java.lang.String s1; String s2; public static void main(String[] args) { HelloWorld.main(null); } }
A subdirectory for each word between dots.
Although we see in the Eclipse Package Explorer something like;
where it seems that there is a "com.edu4java.javatutorials" directory where the file "HelloWorld.java" is, the reality is that "HelloWorld.java" is in:
C:\eclipse\workspace\hello-world\src\com\edu4java\javatutorials\HelloWorld.java
Packages affect visibility of methods, fields...
The methods and fields of a class are accessible from another class in the same package, unless they are declared using the private modifier. We will see more about the access modifiers (public, private, and protected) in a future tutorial.