1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

Packages

Packages are useful organizing structures for objects. Here is an example of how you create one.

The source file is placed under a directory that matches the package name. A package declaration goes at the top of the file.

pack/Pack.java
1package pack;
2 
3public class Pack {
4    public static void main(String[] args) {
5        System.out.println("pack!");
6    }
7}
$ javac pack/Pack.java
$ java pack.Pack
pack!

And here is an example of how you use one

UsePack.java
1import pack.Pack;
2 
3public class UsePack {
4    public static void main(String[] args) {
5        Pack.main(args);
6    }
7}
$ javac UsePack.java
$ java UsePack
pack!

The import statement makes the class available to us.

UsePack2.java
1public class UsePack2 {
2    public static void main(String[] args) {
3        pack.Pack.main(args);
4    }
5}
$ javac UsePack2.java
$ java UsePack2
pack!

In UsePack2 we avoid the import and just call main by its full name.

Now we use essentially the same class, but put it into a different deeper directory. Note how the "." in the package name maps to a "/" in the file name.

pack/level2/Pack.java
1package pack.level2;
2 
3public class Pack {
4    public static void main(String[] args) {
5        System.out.println("pack!!");
6    }
7}
$ javac pack/level2/Pack.java
$ java pack.Pack
pack!
UsePack3.java
1import pack.level2.Pack;
2 
3public class UsePack3 {
4    public static void main(String[] args) {
5        Pack.main(args);
6        pack.Pack.main(args);
7    }
8}
$ javac UsePack3.java
$ java UsePack3
pack!!
pack!

There is a subtlety here. Note the invocation of "Pack.main(args)" on line 5 calls "pack.level2.Pack.main" because that is the Pack that was imported into the current context.

We also invoke the previously defined "pack.Pack.main" by referring to it by full path name.

UsePack4.java
1import pack.Pack;
2 
3public class UsePack4 {
4    public static void main(String[] args) {
5        pack.level2.Pack.main(args);
6        Pack.main(args);
7    }
8}
$ javac UsePack4.java
$ java UsePack4
pack!!
pack!
$ javap UsePack4
Compiled from "UsePack4.java"
public class UsePack4 extends java.lang.Object{
    public UsePack4();
    public static void main(java.lang.String[]);
}

$ javap pack.Pack
Compiled from "Pack.java"
public class pack.Pack extends java.lang.Object{
    public pack.Pack();
    public static void main(java.lang.String[]);
}

Here we reverse the class that we import and the one that we invoke by full name. Importing is always optional and only serves to cut down on typing.

Next, we introduce the javap command. Javap shows you what methods and fields are present on a given class.

You will notice a few interesting things here:

  1. UsePack4 extends java.lang.Object: We never specified that UsePack4 extended anything. By default, all classes extend Object if they don't extend anything else.
  2. String, like Object, belongs to the package "java.lang." You don't need to import the java.lang package. It is there by default.
UsePack5.java
1import pack.*;
2 
3public class UsePack5 {
4    public static void main(String[] args) {
5        pack.level2.Pack.main(args);
6        Pack.main(args);
7    }
8}
$ javac UsePack5.java
$ java UsePack5
pack!!
pack!

You can use * to import all classes in a given package.

UsePack6.java
1import static java.lang.Math.*;
2import static java.lang.System.out;
3 
4public class UsePack6 {
5    public static void main(String[] args) {
6        out.println("sqrt(2) is "+sqrt(2));
7    }
8}
$ javac UsePack6.java
$ java UsePack6
sqrt(2) is 1.4142135623730951

Finally, there is the concept of static imports. These allow us to effectively import the static fields and methods of some other class.

They are especially useful for the mathematical functions in java.lang.Math (it is a pain to type Math.sqrt() when you want sqrt()).

functions