CBOP3203
A class library is a set of classes that supports the development of programs. Java contains an extensive library of pre- written classes you can use in your programs. These classes are divided into groups called packages . Each package defines a number of classes, interfaces, exceptions, and errors. Packages can be split into sub-packages. For example, the java.lang package has a sub- package called java.lang.reflect.
Package • Groups class definitions together for privileged access, lets other classes in the package see more details. • Generally a way of grouping a set of library routines. Example packages from Java’s standard class library: Package Class Use java.util Scanner Inputting from keyboard and files Random Generates random numbers java.lang Math Various math constants and functions String Handling text java.text DecimalFormat Formatting decimal numbers
Classes and Java library packages Classes and Java library packages
Using Package ◦ There are two ways we can use public class in a package: 1. Using its absolute name; and 2. Using the keyword import. Using Absolute Name java.util.Date DATE = new java.util.Date(); ◦ Using import Keyword import java.util.*; ◦ Date DATE = new Date(); ◦
To use a package in a program, it must first be imported. Add the import declaration at the beginning of a program, after the package statement and before any other executable code. ◦ import packageName.*; Imports all classes in packageName. ◦ import packageName.className; Imports one className from packageName ◦ Example: import java.text.DecimalFormat; Imports only the DecimalFormat class from the java.text package. ◦ The java.lang package does not have to be imported. String and Math methods are always available.
//in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . }
package first; classA.java class classA { Being declared in classB b = … new classB(); the same package } will allow each class to access each other without extra work. classA using g classB package first; classB.java class classB { // most of your code }
package first; import second; classA.java class classA { Being declared in classB b = … new classB(); the different package } will require the use of import to access each other. package second; classB.java class classB { // most of your code }
ja java va.l .lan ang ◦ Provides classes that are fundamental to the design of the Java programming language. ◦ The compiler for each Java program automatically calls this package. It consists of the basic classes for java language like object, String and System. It also consists of wrapper classes like Integer, Characters and Float that can be used to change the data from the primitive object.
ja java va.uti util ◦ The package consists of general-purpose classes to be used in a class. The general-purpose classes are general data structure, bit class, time, date, day, string manipulation, random number generator, etc. ja java va.io io ◦ This package provides classes that deals with input stream and output to be read and printed into a file, stream and other resources like network socket.
java.net ◦ This package can support network application like URL, TCP socket, UDP socket, IP address and also binary to text conversion. java.awt ◦ This package provides interface characteristic like window, dialog box, button, choices box, list, menu, scroll bar and test space. For more details of java packages visit: http://java.sun.com/javase/7/docs/api/ Or http://doc.java.sun.com/DocWeb/
Q1. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? Q2. Create a java package with two classes to display a message entered by user. You should use the Scanner class in java class library to get the message from the user.
Recommend
More recommend