Packages and Imports

Dr Andy Evans

[Fullscreen]

Review

  • Classes contain variables and methods.
  • We can turn these into objects containing data and methods.
  • We can limit who sees these using:
    • public :everyone can use it.
    • private :only the code in the class can use it.
    • protected : only inheriting code can use it.
  • We can also set them as static - meaning we can use the methods without making an object.

Review

  • We can turn (super)classes into subclasses.
    • E.g. a Point class can be used to make a Land class.
  • Subclasses invisibly pick up all the superclass code and can use the public bits.
  • Alternatively we can use Interfaces, which are a set of promises.
  • extends : you get everything.
  • implements : you promise everything.
  • If a method expects a parent type, it can take a subclass in, but can only use parent methods and variables.

Packages

  • Packages are groups of classes.
  • Often you put an application in its own package.
  • Also 'libraries' of useful classes.
  • Packages allow us to use other people's code (saves name clashes with other people).

Namespace

  • Packages are named after the directory they are in.
  • Directory structures should reflect your internet address.
  • This means we know where classes are on our machine, and who wrote them.
  • You should build up package hierarchies. E.g...
    	
    /uk/ac/leeds/mass/
    /uk/ac/leeds/mass/aevans/
    
    
  • One package for everyone, one specifically for aevans' use. Note that directories can't just start with numbers. All package names and directories should be lowercase.
  • For the curious: MASS

Naming packages

  • The name is each directory name separated by dots:
    	
    /uk/ac/leeds/mass/
    uk.ac.leeds.mass
    
    
  • A class is declared as being in a package before its class declaration:
    	
    package uk.ac.leeds.mass;
    
    public class MyClass {
    }		
    
    
  • All package names and directories should be lowercase.

Using packages

  • If we want to use a class we haven't written, we need to import it:
    	
    package uk.ac.leeds.mass;
    
    import java.awt.Point;
    
    class MyClass {
        public static void main (String args[]) {
    		Point p = new Point();
        }
    }
    
    
  • Equally we can import a whole package if we want to use several Classes in it...
    	
    import java.awt.*;
    
    
  • No major overhead - just directs compiler to find the files, doesn't put them all in your program.
  • Alternative is to give the full classname:
    	
    java.awt.Point a = new java.awt.Point();
    
    
    But this only really worthwhile if two imported packages have the same class name in them.
  • Note, though, not hierarchical:
    	
    import java.awt.*;
    
    
    Doesn't import:
    	
    import java.awt.events.*;
    
    

Compiling and running

  • How do the compiler and interpreter know where different packages are?
  • java.lang invisibly available (e.g. java.lang.System).
  • Other core java packages need importing, but found automatically.
  • For others, they look in the CLASSPATH environment variable for the directories where they should start looking for the tree. This path usually contains ".", which directs them to the current directory.

Directory trees


[Fullscreen]

If we want to compile a class in presidents we have to run the compiler from the src directory, with the command...


javac us\gov\presidents\Obama.java 

or

javac us\gov\presidents\*.java

to compile everything.

If we use a uk.ac.leeds.geog class in Obama, that will also be found because the directory tree starts the same place.
To run we use the full name of the main class:


java us.gov.presidents.Obama 

CLASSPATH

  • But what if the trees don't start in the same place?
    We set the environmental variable 'CLASSPATH'.
  • Environmental variables are just variables all computers keep which store user preferences. You can tell the 'javac' and 'java' programs the classpath using...
    	
    javac -classpath path;path; File.java
    	(in UNIX: path:path)
    
    
  • The programs will look in any directory listed in the CLASSPATH for the start of directory trees.
  • One path should be "." if you want to include the present directory.
  • Note that if your code is unpackaged this can become quite complicated, as the classpath must include here your code is running from, and the root of the hierarchy. Assuming you are in the latter, you'll need this kind of thing:
    	
    javac unpackaged/lazyCode/*.java
    java  -classpath unpackaged/lazyCode/;. MyClass
    
    
  • What you CAN'T do is this:
    	
    java unpackaged/lazyCode/MyClass
    
    
    The JVM will just assume you're trying to run from a package.

IDEs/jar

  • Most IDEs will put files in their own structure.
  • They will construct the -classpath option suitably when they run.
  • They generally also allow you to add extra libraries, and sort out the classpath for you.
  • These are often in the form of jar files. Jar files are zip files with an extra text file. IDEs sometimes generate these from your code, as you can set them to run when double clicked in Windows etc.
  • You can make them yourself using the jar tool.

Example: Array alternatives

  • 'Collection' classes in java.util that allow you to store stuff.
  • Pros: these are like arrays, but store anything, and expand automatically.
  • Cons: tend to be less efficient than arrays and can take up a lot of space if you're not careful.
	
java.util.Vector
java.util.ArrayList
java.util.Stack
java.util.Hashtable
java.util.Properties
java.util.Bitset
java.util.Enumeration (interface)

Objects

  • How do they store anything?
  • Most objects we create automatically inherit from a built in class called java.lang.Object.
  • This is useful, because it allows us to make storage classes that can store anything.
  • Their storage methods take in generic objects.
  • This means we have to cast anything in them back to the proper class.

Vector

  • Like an array, however, of variable size.
    	
    Vector()
    Vector(int size)
    Vector(int size, int increment)
    
    
  • When full, the Vector doubles in size, or increases by the increment given.

Vector methods

	
addElement(Object ob) 
insertElementAt(Object ob, int index)
// Add elements in at the top or index position.

elementAt(int index)
// Gets an element at the index position.

contains(Object ob)
// Checks the object is in the Vector.

Example

	
import java.util.*;

public class GIS {
    Vector store = new Vector(5,5);
    public GIS () {
		Point pOne = new Point();
		store.add(p, 10);
		// some time later
		for (int i = 0; i < store.size(); i++) {
		    Point p1 = (Point) store.elementAt(10);
		}
    }
}

Review

  • To use others' code:
    • Import the package/classes.
    • Make sure you are compiling from the right location, or set the classpath.