1 / 21

Packages, JARs and Access Modifiers

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.

Download Presentation

Packages, JARs and Access Modifiers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Packages, JARs and Access Modifiers Chris Loftus

  2. 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

  3. 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

  4. 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...

  5. 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...

  6. 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...

  7. Lab: Packages in the Account example (Task 1)

  8. 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

  9. 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)

  10. 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

  11. 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...

  12. 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 “.”

  13. 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...

  14. 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…

  15. 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...

  16. Lab: Finding classes using the Java classpath (Task 2)

  17. 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...

  18. 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)...

  19. 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?...

  20. 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!...

  21. Method access modifiers • public • Visible everywhere • none • Visible in current package • protected • Visible in current package and subclasses • private • Only visible in current class...

More Related