SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
Packages and
 Class path


               1
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
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 won't 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
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
    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
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
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
Example: Placing StudentRecord
class in SchoolClasses pacakge
package SchoolClasses;


public class StudentRecord {
   private String   name;
   private String   address;
   private int      age;
   :




                                 8
Importing and Using
 classes from other
     Packages
                      9
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 member's entire package using import
        statement
    –   Refer to the member by its fully qualified name (without
        using import statement)




                                                                   10
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 haven't
    imported any packages.
●   The syntax for importing packages is as follows:
        import <nameOfPackage>;




                                                                11
Example: Importing a Class or a
           Package
// Importing a class
import java.util.Date;


// Importing all classes in the
// java.util package
import java.util.*;




                                  12
Using Classes of other packages
     via fully qualified path

public static void main(String[] args) {
    java.util.Date x = new java.util.Date();
}




                                               13
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
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
     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
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
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
Class path

             19
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
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 don't set
    the classpath.

                                                                 21
Setting the CLASSPATH
   ●   Suppose we compile and then run the StudentRecord
       class we wrote in the last section,
C:schoolClasses>javac StudentRecord.java

C:schoolClasses>java StudentRecord
Exception 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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Packages in java
Packages in javaPackages in java
Packages in java
 
5.interface and packages
5.interface and packages5.interface and packages
5.interface and packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
Java program structure
Java program structure Java program structure
Java program structure
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Java program structure
Java program structureJava program structure
Java program structure
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java packages
Java packagesJava packages
Java packages
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
java packages
java packagesjava packages
java packages
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 

Ähnlich wie javapackage

7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .happycocoman
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packagesKuntal Bhowmick
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)Narayana Swamy
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in javaPACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in javaJayanthiM19
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .pptthenmozhip8
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - JavaDaniel Ilunga
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptxKavitha713564
 
Packages in java
Packages in javaPackages in java
Packages in javajamunaashok
 

Ähnlich wie javapackage (20)

7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Java Packages
Java Packages Java Packages
Java Packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Z blue interfaces and packages (37129912)
Z blue   interfaces and  packages (37129912)Z blue   interfaces and  packages (37129912)
Z blue interfaces and packages (37129912)
 
Java packages
Java packagesJava packages
Java packages
 
Packages
PackagesPackages
Packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
9 cm604.26
9 cm604.269 cm604.26
9 cm604.26
 
Packages
PackagesPackages
Packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in javaPACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
PACKAGES, MULTITHREADED PROGRAMMING & MANAGING ERRORS AND EXCEPTIONS in java
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Mehr von Arjun Shanka (20)

Asp.net w3schools
Asp.net w3schoolsAsp.net w3schools
Asp.net w3schools
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Sms several papers
Sms several papersSms several papers
Sms several papers
 
Jun 2012(1)
Jun 2012(1)Jun 2012(1)
Jun 2012(1)
 
System simulation 06_cs82
System simulation 06_cs82System simulation 06_cs82
System simulation 06_cs82
 
javaexceptions
javaexceptionsjavaexceptions
javaexceptions
 
javainheritance
javainheritancejavainheritance
javainheritance
 
javarmi
javarmijavarmi
javarmi
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
hibernate
hibernatehibernate
hibernate
 
javaarray
javaarrayjavaarray
javaarray
 
swingbasics
swingbasicsswingbasics
swingbasics
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
 
struts
strutsstruts
struts
 
javathreads
javathreadsjavathreads
javathreads
 
javabeans
javabeansjavabeans
javabeans
 
72185-26528-StrutsMVC
72185-26528-StrutsMVC72185-26528-StrutsMVC
72185-26528-StrutsMVC
 
javanetworking
javanetworkingjavanetworking
javanetworking
 
javaiostream
javaiostreamjavaiostream
javaiostream
 
servlets
servletsservlets
servlets
 

Kürzlich hochgeladen

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 

Kürzlich hochgeladen (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 

javapackage

  • 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. 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 won't 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
  • 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. 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. 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. Example: Placing StudentRecord class in SchoolClasses pacakge package SchoolClasses; public class StudentRecord { private String name; private String address; private int age; : 8
  • 9. Importing and Using classes from other Packages 9
  • 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 member's entire package using import statement – Refer to the member by its fully qualified name (without using import statement) 10
  • 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 haven't imported any packages. ● The syntax for importing packages is as follows: import <nameOfPackage>; 11
  • 12. Example: Importing a Class or a Package // Importing a class import java.util.Date; // Importing all classes in the // java.util package import java.util.*; 12
  • 13. Using Classes of other packages via fully qualified path public static void main(String[] args) { java.util.Date x = new java.util.Date(); } 13
  • 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. Managing Sources & Class files 15
  • 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. 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. 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
  • 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. 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 don't set the classpath. 21
  • 22. Setting the CLASSPATH ● Suppose we compile and then run the StudentRecord class we wrote in the last section, C:schoolClasses>javac StudentRecord.java C:schoolClasses>java StudentRecord Exception 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. 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. 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. 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