SlideShare a Scribd company logo
1 of 26
Packages and Classpath


Objective :

  On completion of this period, you would be
  able to learn
  • Packages
  • Concept of classpath




                                               1
Recap



Final class
• Classes that can not be inherited




                                      2
Packages

• What is a package?
  • We already know how to organize our
    files
  • We use directories for doing so
  • The advantage is
      • Easy maintenance of files
  • If we want to organize our java class files
      • System provides a facility
         • Packages

                                                  3
Packages    Contd..


• Package
  • Name provided to a group of classes
  • A collection of related classes and
    interfaces
  • A namespace management tool provided
    by Java



                                           4
Packages       Contd..
• Directory structures that organize classes and
  interfaces
• Mechanism for software reuse
• Can be compared to directories or folders


• Files : Directories :: Class files : Packages




                                                   5
                                                       5
Naming Convention

•   Follows standard Java naming rules
•   Case sensitive
•   Generally written in small case
•   Every name must be unique
•   Packages in the Java language itself begin with
    java




                                                  6
Naming Convention Contd..

• Prefix a domain name to the preferred package
  name
   • Avoids name conflict when used in Internet
   • for example, com.company.region.package
      • com.ibm.in.banking




                                                  7
Packages


Java packages are classified in to two types :
• Java API packages ( system defined packages)
• User defined packages




                                                 8
Packages       Contd..
•The packages are organized in a hierarchical structure as
shown in the figure

          java. awt

                               Package containing awt package
          COLOUR

         GRAPHICS
                                Package containing AWT classes

           FONT
            …




          IMAGE                   AWT classes containg methods
                          Hierarchical representation of java.awt
                          package. The package named java contains the
                          package awt , which in turn contains various
        Fig. 26.1         classes required for implementing graphical
                          user interface
                                                                   9
System defined packages
• Some examples
  • java.io
  • java.util
  • java.lang
  • java.awt
  • java.awt.event
  • java.sql
  • java.net
  • java.math
  • java.io
                                   10
Package Statement
• Used to create a package
• Should be the first statement in a Java source
  file
• Syntax
  – package nameOfPackage;

  Keyword
                 User-defined name


 • eg. : package cis; // cis – college information system
 • nameOfPackage is a sequence of names separated by .
 (dot)

                                                            11
Package Statement Contd..
• Java supports the package hierarchy
• Specified by multiple names in a package
  statement, separated by . (dot)

  – eg. : package firstPackage.secondPackage;

• Advantage is
  – Group related classes into a package and
    then group the related packages into a larger
    package
                                                12
Packages
Example :
package firstPpackage ; // package declanation
public class firstClass // class definition
{
    ----------- ( body of class )
    -----------
 }
• Here the package name is ‘firstPackage’
• The class ‘firstClass’ is now considered a part of this
   package
• This listing would be saved as a file called firstClass.java
• When the source file is compiled, java compiler will
  create a class file and store it in the ‘firstPackage’
  directory                                            13
Example Program
// A simple package
package coreBank;

class Balance {                  package statement

 String name;
 double bal;

 Balance(String n, double b) {
   name = n;
   bal = b;
 }
                                                     14
Example Program Contd..
void show() {
    if(bal<0)
      System.out.print("-->> ");
      System.out.println(name + ": $" + bal);
  }
}




                                                15
Example Program Contd..
 package coreBank;
 public class AccountBalance {
    public static void main(String args[]) {
      Balance current[] = new Balance[3];
      current[0] = new Balance("K. J. Fielding", 123.23);
      current[1] = new Balance("Will Tell", 157.02);
      current[2] = new Balance("Tom Jackson", -12.33);
      for(int i=0; i<3; i++) current[i].show();
  }
}



                                                      16
Compiling and Executing
• On compilation
  • The package statement creates
    directory(folder) with the name of the package
  • That is, folder named ‘coreBank’ is created
• While executing
  • Qualify the class name with its package name
  • That is, coreBank. AccountBalance




                                                17
Compiling and Executing                   Contd..

• Compiling
  • javac AccountBalance.java

• Executing
  • java coreBank.AccountBalance


         Class name to be qualified with its package




                                                         18
Default Package

• System provides when you do not create one
• It is the current working directory




                                               19
Advantages of Packages


• Easily determine how classes and
  interfaces are related
• Avoids namespace conflict
• Implement       application-wise  access
  protection
• Provides a mechanism for class reuse




                                             20
Summary
• In this class we have discussed
   • Package
   • Naming convention
   • Creating a package
   • Concepts of classpath




                                    21
Frequently Asked Questions
1. What is a package ?

2. What are Java’s system defined packages ?

3. What are the advantages of packages ?

4. How to set classpath for your Java
   application ?


                                           22
Quiz

1.An package is a collection of

  1. classes
  2. Interfaces
  3. editing tools
  4. classes and interfaces




                                  23
Quiz             Contd..




2.Which keyword is used to define package

  1. packages
  2. package
  3. packaging
  4. directory




                                            24
Quiz               Contd..




3.A class path specifies

  1. A list of paths for java source files
  2. A list of paths for image files
  3. A list of paths for the required classes and
     zip files
  4. None



                    CM604.26                     25
Quiz              Contd..

4.Which of the following statements is true ?

  1. There can be any number of package
     statements in a java file
  2. Package statement can be any where in the
     java file
  3. There can be only one package statement in
     a java file
  4. None



                      CM604.26                    26

More Related Content

What's hot

OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class StructureFernando Gil
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packagesTharuniDiddekunta
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersRatnaJava
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)It Academy
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course RAKESH P
 
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa AptechSession 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa AptechMasterCode.vn
 
Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGipradeepfn
 

What's hot (20)

Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages in java
Packages in javaPackages in java
Packages in java
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
 
Java packages
Java packagesJava packages
Java packages
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course
 
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa AptechSession 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
 
Java basics
Java basicsJava basics
Java basics
 
Introduction to OSGi
Introduction to OSGiIntroduction to OSGi
Introduction to OSGi
 
Java Tutorial 1
Java Tutorial 1Java Tutorial 1
Java Tutorial 1
 

Viewers also liked

Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5dplunkett
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7dplunkett
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 Bdplunkett
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9dplunkett
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2dplunkett
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6dplunkett
 
Ap Power Point Chpt1
Ap Power Point Chpt1Ap Power Point Chpt1
Ap Power Point Chpt1dplunkett
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java StatementsFernando Gil
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java SlidesVinit Vyas
 

Viewers also liked (11)

Ap Power Point Chpt5
Ap Power Point Chpt5Ap Power Point Chpt5
Ap Power Point Chpt5
 
Ap Power Point Chpt7
Ap Power Point Chpt7Ap Power Point Chpt7
Ap Power Point Chpt7
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
Java pdf
Java   pdfJava   pdf
Java pdf
 
Ap Power Point Chpt9
Ap Power Point Chpt9Ap Power Point Chpt9
Ap Power Point Chpt9
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Ap Power Point Chpt1
Ap Power Point Chpt1Ap Power Point Chpt1
Ap Power Point Chpt1
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java Statements
 
Core Java Slides
Core Java SlidesCore Java Slides
Core Java Slides
 

Similar to Packages and Classpath Explained

Similar to Packages and Classpath Explained (20)

Java Packages
Java Packages Java Packages
Java Packages
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages unit 5 .ppt
packages  unit 5 .pptpackages  unit 5 .ppt
packages unit 5 .ppt
 
Packages
PackagesPackages
Packages
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Pi j4.1 packages
Pi j4.1 packagesPi j4.1 packages
Pi j4.1 packages
 
javapackage
javapackagejavapackage
javapackage
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Chapter 1 :
Chapter 1 : Chapter 1 :
Chapter 1 :
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
Java packags
Java packagsJava packags
Java packags
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Packages and Classpath Explained

  • 1. Packages and Classpath Objective : On completion of this period, you would be able to learn • Packages • Concept of classpath 1
  • 2. Recap Final class • Classes that can not be inherited 2
  • 3. Packages • What is a package? • We already know how to organize our files • We use directories for doing so • The advantage is • Easy maintenance of files • If we want to organize our java class files • System provides a facility • Packages 3
  • 4. Packages Contd.. • Package • Name provided to a group of classes • A collection of related classes and interfaces • A namespace management tool provided by Java 4
  • 5. Packages Contd.. • Directory structures that organize classes and interfaces • Mechanism for software reuse • Can be compared to directories or folders • Files : Directories :: Class files : Packages 5 5
  • 6. Naming Convention • Follows standard Java naming rules • Case sensitive • Generally written in small case • Every name must be unique • Packages in the Java language itself begin with java 6
  • 7. Naming Convention Contd.. • Prefix a domain name to the preferred package name • Avoids name conflict when used in Internet • for example, com.company.region.package • com.ibm.in.banking 7
  • 8. Packages Java packages are classified in to two types : • Java API packages ( system defined packages) • User defined packages 8
  • 9. Packages Contd.. •The packages are organized in a hierarchical structure as shown in the figure java. awt Package containing awt package COLOUR GRAPHICS Package containing AWT classes FONT … IMAGE AWT classes containg methods Hierarchical representation of java.awt package. The package named java contains the package awt , which in turn contains various Fig. 26.1 classes required for implementing graphical user interface 9
  • 10. System defined packages • Some examples • java.io • java.util • java.lang • java.awt • java.awt.event • java.sql • java.net • java.math • java.io 10
  • 11. Package Statement • Used to create a package • Should be the first statement in a Java source file • Syntax – package nameOfPackage; Keyword User-defined name • eg. : package cis; // cis – college information system • nameOfPackage is a sequence of names separated by . (dot) 11
  • 12. Package Statement Contd.. • Java supports the package hierarchy • Specified by multiple names in a package statement, separated by . (dot) – eg. : package firstPackage.secondPackage; • Advantage is – Group related classes into a package and then group the related packages into a larger package 12
  • 13. Packages Example : package firstPpackage ; // package declanation public class firstClass // class definition { ----------- ( body of class ) ----------- } • Here the package name is ‘firstPackage’ • The class ‘firstClass’ is now considered a part of this package • This listing would be saved as a file called firstClass.java • When the source file is compiled, java compiler will create a class file and store it in the ‘firstPackage’ directory 13
  • 14. Example Program // A simple package package coreBank; class Balance { package statement String name; double bal; Balance(String n, double b) { name = n; bal = b; } 14
  • 15. Example Program Contd.. void show() { if(bal<0) System.out.print("-->> "); System.out.println(name + ": $" + bal); } } 15
  • 16. Example Program Contd.. package coreBank; public class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance("K. J. Fielding", 123.23); current[1] = new Balance("Will Tell", 157.02); current[2] = new Balance("Tom Jackson", -12.33); for(int i=0; i<3; i++) current[i].show(); } } 16
  • 17. Compiling and Executing • On compilation • The package statement creates directory(folder) with the name of the package • That is, folder named ‘coreBank’ is created • While executing • Qualify the class name with its package name • That is, coreBank. AccountBalance 17
  • 18. Compiling and Executing Contd.. • Compiling • javac AccountBalance.java • Executing • java coreBank.AccountBalance Class name to be qualified with its package 18
  • 19. Default Package • System provides when you do not create one • It is the current working directory 19
  • 20. Advantages of Packages • Easily determine how classes and interfaces are related • Avoids namespace conflict • Implement application-wise access protection • Provides a mechanism for class reuse 20
  • 21. Summary • In this class we have discussed • Package • Naming convention • Creating a package • Concepts of classpath 21
  • 22. Frequently Asked Questions 1. What is a package ? 2. What are Java’s system defined packages ? 3. What are the advantages of packages ? 4. How to set classpath for your Java application ? 22
  • 23. Quiz 1.An package is a collection of 1. classes 2. Interfaces 3. editing tools 4. classes and interfaces 23
  • 24. Quiz Contd.. 2.Which keyword is used to define package 1. packages 2. package 3. packaging 4. directory 24
  • 25. Quiz Contd.. 3.A class path specifies 1. A list of paths for java source files 2. A list of paths for image files 3. A list of paths for the required classes and zip files 4. None CM604.26 25
  • 26. Quiz Contd.. 4.Which of the following statements is true ? 1. There can be any number of package statements in a java file 2. Package statement can be any where in the java file 3. There can be only one package statement in a java file 4. None CM604.26 26