Packages and Class path               1
What are Packages?●   A package is a grouping of related types providing    access protection and name space management   ...
Benefits of Packaging●   You and other programmers can easily determine that    these classes and interfaces are related.●...
Creating a Package                     4
Creating a Package●   To create a package, you choose a name for the    package and put a package statement with that name...
Placing a Class in a Package●   To place a class in a package, we write the following as    the first line of the code (ex...
Example Package●   Suppose you write a group of classes that represent    graphic objects, such as circles, rectangles, li...
Example: Placing StudentRecordclass in SchoolClasses pacakgepackage SchoolClasses;public class StudentRecord {   private S...
Importing and Using classes from other     Packages                      9
Using Classes from Other                 Packages●   To use a public package member (classes and    interfaces) from outsi...
Importing Packages●   To be able to use classes outside of the package you are    currently working in, you need to import...
Example: Importing a Class or a           Package// Importing a classimport java.util.Date;// Importing all classes in the...
Using Classes of other packages     via fully qualified pathpublic static void main(String[] args) {    java.util.Date x =...
Package & Directory Structure●   Packages can also be nested. In this case, the Java    interpreter expects the directory ...
Managing Sources  & Class files                   15
Managing Source and Class Files●   Create a *.java file for the class you want to create     // in the Rectangle.java file...
Directory Structure of Java Source               Files●   The qualified name of the class file and the path name    to the...
Directory Structure of Java Source               Files●   However, the path to the .class files does not have to    be the...
Class path             19
What is a class path?●   It is a set of directories where Java class files are    located●   Java runtime searches for cla...
Setting the CLASSPATH●   Now, suppose we place the package schoolClasses    under the C: directory.●   We need to set the ...
Setting the CLASSPATH   ●   Suppose we compile and then run the StudentRecord       class we wrote in the last section,C:s...
Setting the CLASSPATH●   To set the classpath in Windows, we type this at the    command prompt,    C:schoolClasses> set c...
Setting the CLASSPATH●   For Unix base systems, suppose we have our classes    in the directory /usr/local/myClasses, we w...
Setting the CLASSPATH●   Take note that you can set the classpath anywhere.    You can also set more than one classpath, w...
Upcoming SlideShare
Loading in...5
×

javapackage

1,388

Published on

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,388
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
115
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

javapackage

  1. 1. Packages and Class path 1
  2. 2. What are Packages?● A package is a grouping of related types providing access protection and name space management – Note that types refers to classes, interfaces, enumerations, and annotation types. – Types are often referred to simply as classes and interfaces since enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to simply as classes and interfaces. 2
  3. 3. Benefits of Packaging● You and other programmers can easily determine that these classes and interfaces are related.● You and other programmers know where to find classes and interfaces that can provide graphics- related functions.● The names of your classes and interfaces wont conflict with the names in other packages because the package creates a new namespace.● You can allow classes within the package to have unrestricted access to one another yet still restrict access for types outside the package. 3
  4. 4. Creating a Package 4
  5. 5. Creating a Package● To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package● If you do not use a package statement, your type ends up in an unnamed package – Use an unnamed package only for small or temporary applications 5
  6. 6. Placing a Class in a Package● To place a class in a package, we write the following as the first line of the code (except comments) package <packageName>; package myownpackage; 6
  7. 7. Example Package● Suppose you write a group of classes that represent graphic objects, such as circles, rectangles, lines, and points● You also write an interface, Draggable, that classes implement if they can be dragged with the mouse //in the Draggable.java file public interface Draggable {} //in the Graphic.java file public abstract class Graphic {} //in the Circle.java file public class Circle extends Graphic implements Draggable {} //in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { } //in the Point.java file public class Point extends Graphic implements Draggable {} //in the Line.java file public class Line extends Graphic implements Draggable {} 7
  8. 8. Example: Placing StudentRecordclass in SchoolClasses pacakgepackage SchoolClasses;public class StudentRecord { private String name; private String address; private int age; : 8
  9. 9. Importing and Using classes from other Packages 9
  10. 10. Using Classes from Other Packages● To use a public package member (classes and interfaces) from outside its package, you must do one of the following – Import the package member using import statement – Import the members entire package using import statement – Refer to the member by its fully qualified name (without using import statement) 10
  11. 11. Importing Packages● To be able to use classes outside of the package you are currently working in, you need to import the package of those classes.● By default, all your Java programs import the java.lang.* package, that is why you can use classes like String and Integers inside the program even though you havent imported any packages.● The syntax for importing packages is as follows: import <nameOfPackage>; 11
  12. 12. Example: Importing a Class or a Package// Importing a classimport java.util.Date;// Importing all classes in the// java.util packageimport java.util.*; 12
  13. 13. Using Classes of other packages via fully qualified pathpublic static void main(String[] args) { java.util.Date x = new java.util.Date();} 13
  14. 14. Package & Directory Structure● Packages can also be nested. In this case, the Java interpreter expects the directory structure containing the executable classes to match the package hierarchy.● There should have same directory structure, ./myowndir/myownsubdir/myownpackage directory for the following package statement package myowndir.myownsubdir.myownpackage; 14
  15. 15. Managing Sources & Class files 15
  16. 16. Managing Source and Class Files● Create a *.java file for the class you want to create // in the Rectangle.java file package graphics; public class Rectangle() { ... }● Place the source file in a directory whose name reflects the name of the package to which the class belongs – .....graphicsRectangle.java 16
  17. 17. Directory Structure of Java Source Files● The qualified name of the class file and the path name to the Java source file are parallel● Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name● Example class name: graphics.Rectangle pathname to source file: graphics/Rectangle.java pathname to the class file: graphics/Rectangle.class 17
  18. 18. Directory Structure of Java Source Files● However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as: <path_one>sourcescomexamplegraphicsRectangle.java <path_two>classescomexamplegraphicsRectangle.class● By doing this, you can give the classes directory to other programmers without revealing your sources● You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses 18
  19. 19. Class path 19
  20. 20. What is a class path?● It is a set of directories where Java class files are located● Java runtime searches for class files in the directories specified in the class path in the order specified 20
  21. 21. Setting the CLASSPATH● Now, suppose we place the package schoolClasses under the C: directory.● We need to set the classpath to point to that directory so that when we try to run it, the JVM will be able to see where our classes are stored.● Before we discuss how to set the classpath, let us take a look at an example on what will happen if we dont set the classpath. 21
  22. 22. Setting the CLASSPATH ● Suppose we compile and then run the StudentRecord class we wrote in the last section,C:schoolClasses>javac StudentRecord.javaC:schoolClasses>java StudentRecordException in thread "main" java.lang.NoClassDefFoundError: StudentRecord(wrong name: schoolClasses/StudentRecord) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) 22
  23. 23. Setting the CLASSPATH● To set the classpath in Windows, we type this at the command prompt, C:schoolClasses> set classpath=C: – assuming C: is the directory in which we have placed the packages meaning there is a directory C:schoolClasses and there is a C:schoolClassesStudentRecord.class● After setting the classpath, we can now run our program anywhere by typing, C:schoolClasses> java schoolClasses.StudentRecord 23
  24. 24. Setting the CLASSPATH● For Unix base systems, suppose we have our classes in the directory /usr/local/myClasses, we write, export classpath=/usr/local/myClasses 24
  25. 25. Setting the CLASSPATH● Take note that you can set the classpath anywhere. You can also set more than one classpath, we just have to separate them by ;(for windows) and : (for Unix based systems). For example, set classpath=C:myClasses;D:;E:MyProgramsJava● and for Unix based systems, export classpath=/usr/local/java:/usr/myClasses 25
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×