210 likes | 317 Views
Packages, JARs and Access Modifiers. Chris Loftus. Packages. Why do we use them? Group associated classes providing a logical separation helping maintenance and improve design... In Java cannot import from default package... In Java a class can only belong to one package.
E N D
Packages, JARs and Access Modifiers Chris Loftus
Packages • Why do we use them? • Group associated classes providing a logical separation helping maintenance and improve design... • In Java cannot import from default package... • In Java a class can only belong to one package
Roladex demo (notes) • Let’s make some changes to the Roladex application to add packages... • Created a package called uk.ac.aber.dcs.roladex • Actually this creates five packages • Why do this? Uniquely named using domain name… • Fully qualified name for Roladex is now uk.ac.aber.dcs.roladex.Roladex
Roladex demo (notes cont) • Dragged and dropped the original roladex classes into the roladex package... • Looked at created package statement in one of the classes • It must be the first statement • Created a tests package under the roladex package...(explained why in class) • Dragged and dropped the original test classes into the tests package...
Roladex demo (notes cont) • Opened up RoladexTest class • Package statement included • Two imports to Contact and Roladex included automatically... • If you want to use a class from a different package (even an ancestor package) then you either have to import it or type in the fully qualified name... • Ran the JUnit tests to make sure nothing broken...
Java libraries • These are organised in packages and you’ve been using them: • e.g. import java.util.ArrayList • Any Java class defined in the package java.lang does not need to be imported, e.g. String • All classes belong to a package • If not explicitly defined then called the default package...We expect you to use explicit packages from now on...
Packages (notes cont) • By convention package names are lower case, with underscores to separate words • How do packages correspond to folders/directories? • Let’s look at example that does not use Eclipse • Will use text editor to illustrate (see the example zip file on the website) • Folders must correspond to package names
Packages (notes cont) • Notice how MyClass imports com.company.TheirArrayList • Let’s look at the folder structure that corresponds to this location • However, how do I tell Java where to look for “com” since it could be anywhere in the filesystem?...(next slide)
Java classpath • IDEs typically hide the classpath, but occasionally you need to add libraries to the project classpath, or you need to run outside of an IDE... • Let’s look at: • compile-without-classpath.bat which contains a javac command • Let’s run it. It fails because it can’t find TheirArrayList
Java classpath • We can specify where javac should look for other classes that our classes depend on • Done using classpath argument • Let’s look at: • compile-with-classpath.bat • The –classpath argument provides a list of directory pathnames separated by “;” on Windows and “:” on Unix. In this case only one pathname is needed...
Java classpath • We are telling javac to look in .../exampleCode folder for the com folder... • You must specify the folder that contains the topmost package folder • Here exampleCode contains the com folder • Let’s run the bat/sh file to show it working • We need to do the same when we run the java command... run-with-classpath.bat • Note that in this example I also specify the current working directory “.”
Java classpath • There is also a CLASSPATH environment variable that can be set • In Windows via the Control Panel->System Control • In Unix within a Unix profile script • This is then global to all applications. Avoid this, and use –classpath (-cp) instead...
JAR (Java archive) files • Essentially a ZIP file with a manifest file in the META-INF folder • Let’s Jar up the com.company.TheirClass structure with • jar –cvf util.jar com • Let’s look at the jar file contents with jar –tf util.jar and then open with a zip tool, e.g. WinRar… • Let’s run the run-with-jar.bat file…
Why use JARs? • Better performance when downloading... • Convenient way of distributing Java programs, frameworks and libraries... • You can digitally sign them... • JARs can be placed in • jdk-installation-path\jre\lib\ext...
Access modifiers • What are these? • public, private, protected or none (called the package private modifier) • Let’s look at where they can be applied: class, constructor, instance variable, and method...
Class access modifiers • public • Class is visible everywhere • none • Class is visible in current package only... • This is for top-level classes only and not inner classes. I’m not going to talk about those here (perhaps later)...
Constructor access modifiers • public • Constructor is visible everywhere • none • Constructor is visible in current package • protected • Constructor is visible in current package and subclasses... • private • Constructor is only visible in the same class! Why useful?...
Instance variable access modifiers • public • Visible everywhere. Why is this a bad idea?... • none • Visible in current package only • protected • Visible in current package and in subclasses • private • Visible in current class only • Note that two objects can see each other’s private bits... • NB: protected is weaker than saying nothing at all!...
Method access modifiers • public • Visible everywhere • none • Visible in current package • protected • Visible in current package and subclasses • private • Only visible in current class...