Course List: http://www.c-jump.com/bcc/
We've seen Java API classes being organized in packages. To access them, we ise the import statement. For example,
import java.text.NumberFormat;
Packages organize classes into a logical grouping for importing
The application may also organize its own classes in a set of packages
Classes from nested packages cannot be accessed without being imported
Note: package names must match the path to the subdirectory where each package is stored
For example, assume that a full local path to the class file named DBConnection.java is
C:/c257c/src/bcc/database DBConnection.java
If C:/c257c/src is a root directory for all classes, then the name of the package should be
//NOTE: the package statement must be the first statement in the file
package bcc.database
The import statement for the DBConnection class becomes
import bcc.database.DBConnection;
Create a project that contains just the packages and classes that you want to include in the library
Right-click on the project and select the Build command to compile the project
NetBeans automatically creates a JAR file for the project and stores it in the dist subdirectory for the project
Create or open the project that will use the library
Right-click on the Libraries directory, and
select the Add JAR/Folder command
use the resulting dialog box to select the JAR file for the library
Code the import statements for the packages and classes in the library as needed
Reuse classes stored in the packages
Common HTML tag used to format javadoc comments is
<code></code>
Common javadoc tags are:
@author @version @param @return
The Product class with comments that use HTML and javadoc tags
package murach.business;
/********************************************************
* The <code>Product</code> class represents a product
* and is used by the <code>LineItem</code> and
* <code>ProductDB</code> classes.
* @author Joel Murach
* @version 1.0.0
*******************************************************/
public class Product {
//...
/***************************************************
* Sets the product code to the specified
* <code>String</code>.
* @param code A <code>String</code> for the product
* code.
***************************************************/
public void setCode( String code ){
this.code = code;
}
/***************************************************
* Returns a <code>String</code> that represents the
* product code.
* @return A <code>String</code> for the product
* code.
***************************************************/
public String getCode(){
return code;
}
//...
Two classes declared within a single file:
// file LineItem.java
import java.text.NumberFormat;
public class LineItem
{
private Product product;
private int quantity;
private double total;
//...
}//class LineItem
class Product
{
//...
}//class Product
The generated class files are:
LineItem.class Product.class
An example of two classes nested within another class:
public class OuterClassName { // can contain instance variables and methods // can contain static variables and methods class InnerClassName { // can contain instance variables and methods // can't contain static variables or methods // can access all variables and methods of // OuterClass }//class InnerClassName static class StaticInnerClassName { // can contain instance variables and methods // can contain static variables and methods // can access static variables and methods of // OuterClass // can't access instance variables or methods of // OuterClass }//class StaticInnerClassName }//class OuterClassName
The class files generated for the nested classes are:
OuterClassName.class OuterClassName$InnerClassName.class OuterClassName$StaticInnerClassName.class
An example of a class declared within a method:
public class ClassName { // code for the outer class public void methodName() { class InnerClassName { // code for the inner class }//class InnerClassName // code for the method }//methodName }//class ClassName
The class files generated for this class are:
ClassName.class ClassName$InnerClassName.class
Enumeration is a set of named constants, usually logically related.
Each constant has uniquie transparent ordinal value, such as 0, 1, 2, 3, and so on.
Enumerations can be specified
in a separate file within a package
in a related class file
nested within a class
Enumeration type recognizes each of the named constant, but not the bare ordinal values
The ordinal() method of the Enumeration type provides the actual ordinal value of a particular named constant
The syntax for declaring an enumeration
public enum EnumerationName { CONSTANT_NAME1[, CONSTANT_NAME2]... }
An enumeration that defines three shipping types
public enum ShippingType { UPS_NEXT_DAY, UPS_SECOND_DAY, UPS_GROUND }
A statement that uses the enumeration and one of its constants:
ShippingType secondDay = ShippingType.UPS_SECOND_DAY;
A method that uses the enumeration as a parameter type:
public static double getShippingAmount( ShippingType st ) { double shippingAmount = 2.99; if ( st == ShippingType.UPS_NEXT_DAY ) { shippingAmount = 10.99; } else if ( st == ShippingType.UPS_SECOND_DAY ) { shippingAmount = 5.99; } return shippingAmount; }
A statement that calls the method
double shippingAmount = getShippingAmount( ShippingType.UPS_SECOND_DAY ); // Wrong type, not allowed --- will not compile: double shippingAmount2 = getShippingAmount(1);
Two methods of an enumeration are:
name()
ordinal()
An enumeration that overrides the toString() method:
public enum ShippingType { UPS_NEXT_DAY, UPS_SECOND_DAY, UPS_GROUND; @Override public String toString() { String str = "UNKNOWN"; if ( this.ordinal() == 0 ) { str = "UPS Next Day (1 business day)"; } else if ( this.ordinal() == 1 ) { str = "UPS Second Day (2 business days)"; } else if ( this.ordinal() == 2 ) { str = "UPS Ground (5 to 7 business days)"; } return str; } }
Code that uses the overridden toString() method:
ShippingType ground = ShippingType.UPS_GROUND; System.out.println( "toString: " + ground.toString() + "\n" );
Resulting output:
toString: UPS Ground (5 to 7 business days)
To code a static import statement,
import static murach.business.ShippingType.*;
The code above when a static import is used
ShippingType ground = UPS_GROUND; System.out.println( "toString: " + ground.toString() + "\n" );