Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Programming in Java - 10 - Packages

Packages in Java allow for organizing classes into groups and namespaces. Classes are declared as part of a package and packages can contain subpackages. The code files must specify the package and importing is needed to use classes from other packages. To create a package, the classes are placed into a directory structure that matches the package namespace and that directory is added to the CLASSPATH so it can be found at compile and runtime.

Uploaded by

Reno Tk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Programming in Java - 10 - Packages

Packages in Java allow for organizing classes into groups and namespaces. Classes are declared as part of a package and packages can contain subpackages. The code files must specify the package and importing is needed to use classes from other packages. To create a package, the classes are placed into a directory structure that matches the package namespace and that directory is added to the CLASSPATH so it can be found at compile and runtime.

Uploaded by

Reno Tk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming in Java

Packages

2011 BlueSignet LLC. All rights reserved.

Packages

In Java, packages are a collection of classes


Sort of like a namespace

Behaves the same way

Classes are declared as part of a package


Packages can be broken part by sub packaging
Many Java developers believe that all your code
should belong to a package

2011 BlueSignet LLC. All rights reserved.

How to make a package?

Think of this as a namespace


Insert a package statement at the top of the code
file(s)
Any code file that is using the entities within a
package must either import the package, or
reference the class through the package

2011 BlueSignet LLC. All rights reserved.

Recall the Person and Employee classes


// Person.java
import java.util.Date;
class Person
{
/* Protected Fields */
protected String _firstName = "";
protected String _lastName = "";
/* Private Fields */
protected Date _birthDate = new Date();
/* Public Fields */
public static int _population = 0;
/* Constructors */
public Person() { _population++; }
public Person(String lastName, String firstName)
{
_lastName = lastName;
_firstName = firstName;
_population++;
}
/* Private Methods */
public static int getPopulation() { return _population; }
/* Public Methods -- Getters */
public String getFirstName() { return _firstName; }
public String getLastName() { return _lastName; }
public Date getBirthDate() { return _birthDate; }
/* Public Methods -- Setters */
public void setFirstName(String firstName)
{ _firstName = firstName ; }
public void setLastName(String lastName)
{ _lastName = lastName; }
}

// Employee.java
import java.util.Date;
class Employee extends Person
{
private String _jobTitle = "";
private String _jobTitlePrefix = "";
private String _department = "";
private double _salary = 0.0;
private Date _hireDate = null;
private Date _terminationDate = null;
public Employee(Person p)
{
this.setFirstName(p.getFirstName());
this.setLastName(p.getLastName());
this._birthDate = p._birthDate;
}
public void setJobTitle(String jobTitle)
{ _jobTitle = jobTitle; }
public void setJobTitlePrefix(String jobTitlePrefix)
{ _jobTitlePrefix = jobTitlePrefix; }
public void setDepartment(String department)
{ _department = department; }
public void setSalary(double salary)
{ _salary = salary; }
public void setHireDate(Date hireDate)
{ _hireDate = hireDate; }
public void setTerminationDate(Date terminationDate)
{ _terminationDate = terminationDate; }
public
public
public
public
public
public
}

2011 BlueSignet LLC. All rights reserved.

String getJobTitle() { return _jobTitle; }


String getJobTitlePrefix() { return _jobTitlePrefix; }
String getDepartment() { return _department; }
double getSalary() { return _salary; }
Date getHireDate() { return _hireDate; }
Date getTerminationDate() { return _terminationDate; }

Recall the Person and Employee classes


// Person.java
import java.util.Date;
public class Person
{
/* Protected Fields */
protected String _firstName = "";
protected String _lastName = "";
/* Private Fields */
protected Date _birthDate = new Date();
/* Public Fields */
public static int _population = 0;
/* Constructors */
public Person() { _population++; }
public Person(String lastName, String firstName)
{
_lastName = lastName;
_firstName = firstName;
_population++;
}
/* Private Methods */
public static int getPopulation() { return _population; }
/* Public Methods -- Getters */
public String getFirstName() { return _firstName; }
public String getLastName() { return _lastName; }
public Date getBirthDate() { return _birthDate; }
/* Public Methods -- Setters */
public void setFirstName(String firstName)
{ _firstName = firstName ; }
public void setLastName(String lastName)
{ _lastName = lastName; }
}

// Employee.java
import java.util.Date;
public class Employee extends Person
{
private String _jobTitle = "";
private String _jobTitlePrefix = "";
private String _department = "";
private double _salary = 0.0;
private Date _hireDate = null;
private Date _terminationDate = null;
public Employee(Person p)
{
this.setFirstName(p.getFirstName());
this.setLastName(p.getLastName());
this._birthDate = p._birthDate;
}
public void setJobTitle(String jobTitle)
{ _jobTitle = jobTitle; }
public void setJobTitlePrefix(String jobTitlePrefix)
{ _jobTitlePrefix = jobTitlePrefix; }
public void setDepartment(String department)
{ _department = department; }
public void setSalary(double salary)
{ _salary = salary; }
public void setHireDate(Date hireDate)
{ _hireDate = hireDate; }
public void setTerminationDate(Date terminationDate)
{ _terminationDate = terminationDate; }
public
public
public
public
public
public
}

2011 BlueSignet LLC. All rights reserved.

String getJobTitle() { return _jobTitle; }


String getJobTitlePrefix() { return _jobTitlePrefix; }
String getDepartment() { return _department; }
double getSalary() { return _salary; }
Date getHireDate() { return _hireDate; }
Date getTerminationDate() { return _terminationDate; }

Recall the Person and Employee classes


// Person.java
package person;
import java.util.Date;
public class Person
{
/* Protected Fields */
protected String _firstName = "";
protected String _lastName = "";
/* Private Fields */
protected Date _birthDate = new Date();
/* Public Fields */
public static int _population = 0;
/* Constructors */
public Person() { _population++; }
public Person(String lastName, String firstName)
{
_lastName = lastName;
_firstName = firstName;
_population++;
}
/* Private Methods */
public static int getPopulation() { return _population; }
/* Public Methods -- Getters */
public String getFirstName() { return _firstName; }
public String getLastName() { return _lastName; }
public Date getBirthDate() { return _birthDate; }
/* Public Methods -- Setters */
public void setFirstName(String firstName)
{ _firstName = firstName ; }
public void setLastName(String lastName)
{ _lastName = lastName; }
}

// Employee.java
package person;

import java.util.Date;
public class Employee extends Person
{
private String _jobTitle = "";
private String _jobTitlePrefix = "";
private String _department = "";
private double _salary = 0.0;
private Date _hireDate = null;
private Date _terminationDate = null;
public Employee(Person p)
{
this.setFirstName(p.getFirstName());
this.setLastName(p.getLastName());
this._birthDate = p._birthDate;
}
public void setJobTitle(String jobTitle)
{ _jobTitle = jobTitle; }
public void setJobTitlePrefix(String jobTitlePrefix)
{ _jobTitlePrefix = jobTitlePrefix; }
public void setDepartment(String department)
{ _department = department; }
public void setSalary(double salary)
{ _salary = salary; }
public void setHireDate(Date hireDate)
{ _hireDate = hireDate; }
public void setTerminationDate(Date terminationDate)
{ _terminationDate = terminationDate; }
public
public
public
public
public
public
}

2011 BlueSignet LLC. All rights reserved.

String getJobTitle() { return _jobTitle; }


String getJobTitlePrefix() { return _jobTitlePrefix; }
String getDepartment() { return _department; }
double getSalary() { return _salary; }
Date getHireDate() { return _hireDate; }
Date getTerminationDate() { return _terminationDate; }

Driver Code

// MainClass.java
import person.*;
class MainClass
{
public static void main(String[] args)
{
Employee em = new Employee(new Person(".Net", "WiBit"));
System.out.println(em.getFirstName() + em.getLastName());
}
}

2011 BlueSignet LLC. All rights reserved.

Class Path

Java uses an environment variable called


CLASSPATH to seek java classes
Classes in a package should be contained within
the same directory
This directory should be in a directory path
dedicated for classes

2011 BlueSignet LLC. All rights reserved.

Create Package

Create root class directory

C:\WiBit.Net\Java\Classes

If not already, add this root class directory to the


CLASSPATH environment variable (; delimited)
Create directory for person package
C:\WiBit.Net\Java\Classes\person

Put Person.java and Employee.java in the person


package directory

2011 BlueSignet LLC. All rights reserved.

Create Package - Create root class directory


C:\

>_

WiBit.Net

Java

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Set Class Path (Not Set)


C:\

>echo %CLASSPATH%
%CLASSPATH%
>_

WiBit.Net

Java

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Set Class Path (Not Set)


C:\

>echo %CLASSPATH%
%CLASSPATH%
>set CLASSPATH=.;C:\WiBit.Net\Java\Classes
>echo %CLASSPATH%
.;C:\WiBit.Net\Java\Classes

WiBit.Net

>_
Java

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Compile Person Package


C:\

C:\WiBit.Net\Java\Src>_

WiBit.Net

Java

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Compile Person Package


C:\

C:\WiBit.Net\Java\Src>dir *.java
Volume in drive C has no label.
Volume Serial Number is FA71-1060
Directory of C:\WiBit.Net\Java\Src
04/30/2011
WiBit.Net

03:38 PM
211 MainClass.java
1 File(s)
211 bytes
0 Dir(s) 146,006,347,776 bytes free

C:\WiBit.Net\Java\Src>_
Java

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Compile Person Package


C:\

C:\WiBit.Net\Java\Src>dir *.java
Volume in drive C has no label.
Volume Serial Number is FA71-1060
Directory of C:\WiBit.Net\Java\Src
04/30/2011
WiBit.Net

03:38 PM
211 MainClass.java
1 File(s)
211 bytes
0 Dir(s) 146,006,347,776 bytes free

C:\WiBit.Net\Java\Src>javac MainClass.java
Java

C:\WiBit.Net\Java\Src>_

Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Create Package - Compile Person Package


C:\

C:\WiBit.Net\Java\Src>dir *.java
Volume in drive C has no label.
Volume Serial Number is FA71-1060
Directory of C:\WiBit.Net\Java\Src
04/30/2011
WiBit.Net

03:38 PM
211 MainClass.java
1 File(s)
211 bytes
0 Dir(s) 146,006,347,776 bytes free

C:\WiBit.Net\Java\Src>javac MainClass.java
Java

C:\WiBit.Net\Java\Src>java MainClass
WiBit.Net
C:\WiBit.Net\Java\Src>_
Classes

person
Person.java

Src
Employee.java
MainClass.java

2011 BlueSignet LLC. All rights reserved.

Access the Packaged Classes


class MainClass
{
public static void main(String[] args)
{
person.Employee em =
new person.Employee(new person.Person(".Net", "WiBit"));
System.out.println(em.getFirstName() +
em.getLastName());
}
}

2011 BlueSignet LLC. All rights reserved.

Access the Packaged Classes


import person.Employee;
import person.Person;
class MainClass
{
public static void main(String[] args)
{
Employee em = new Employee(new Person(".Net", "WiBit"));
System.out.println(em.getFirstName() +
em.getLastName());
}
}

2011 BlueSignet LLC. All rights reserved.

The End?
Thank You For Watching!

2011 BlueSignet LLC. All rights reserved.

You might also like