Java software package
Since all Java classes are combined to form a software package, nothing can be done without them.
This chapter first introduces the syntax and semantics of the Java software package, and then discusses some software packages of Java and Visual J ++. Readers may have noticed that, in fact, classes used for programming are included in these predefined software packages. It is more common to use a ready-made software package than to create a new software package. However, sometimes you also need to create your own software package. Here we will illustrate how to create your own software package.
In this article, readers can learn:
* Relationship between software packages and folders;
* Inherent software packages in Java;
* Framework Structure of Windows Foundation Class (WFC, window base class) and WFC software package;
* Create your own software package.
--------------------- The user will create :-----------------------------
* Applications that can read strings from the clipboard and write strings to the clipboard;
* A clock application;
* A utility: contains a simple class that can sum two numbers.
-----------------------------
What is a software package?
A software package is the main organizational entity of Java. It logically combines related classes and defines an access form between classes. Each application has a default software package that contains all the software packages referenced in the code.
Define a class as a member of the named data packet
* The first line in the source code contains the "package" statement:
Package pkgname;
The value of pkgname is the package name, which must match the folder name or directory name of the source code. The following is the package statement of the mystuff package:
Package mystuff;
The package statement must start with the source code and only allow empty lines and comments. The package statement applies to all classes defined in the source code file.
Software packages and file systems
A software package is closely related to a file system, which is the link between a public class and a file.
The public class must be stored in a file with the same name as it. That is to say, the public class named myclass must be stored in the source code file named myclass. java.
Files that contain package statements must also be stored in folders with the same name. For example, the following code indicates that the files named myclass. Java in the folder named mystuff are:
Package mystuff;
Public class myclass
The class file location is also very important. Compile this source code file to create two class files, myclass. Class and myhelper. Class. These class files must be stored in the folder named mystuff:
Package mystuff;
Public class myclass
{
...
}
Class myhelper
{
...
}
Generate two class files: myclass. Class and myhelper. Class. These class files must be in the folder named mystuff. The package name can contain multiple identifiers separated by periods. Each part of the package name contains an independent folder. For example, the following code indicates the myclass file in the folder named mysubpackage, And the mysubpackage folder is in the mystuff folder.
Package mystuff. mysubpackage;
Public class myclass
{
...
}
The name of a software package indicates a specific software package stored in a specific path. There is no special relationship between software packages with the same name in different folders or between classes with the same name in different software packages; in the first example above, the package name is mystuff, and in the second example, the package name is mystuff. mysubpackage. A software package can be stored in the subfolders of another software package, but the classes in these two software packages do not matter. Similarly, mystuff. myclass and mystuff. mysubpackage. there is no special relationship between the two classes.
Class path
A class path is a sequence of folders (or directories). These folders are combined to form a default Software Package for an application; all named software packages can be found in the subfolders of folders in the class path. The package folder is a subfolders of the class path folder. For example, assume that the class path consists of the following folders:
C:/Windows/Java/classes
C:/Windows/Java/lib
C:/mylibrary
Class file C:/Windows/Java/classes/utilities. Class is directly stored in the default software package.
Now, let's assume that several lines of code in the myclass. Class file are compiled to create a class file in the class path:
Package mystuff;
Public class myclass
{
...
}
The mystuff. myclass file may be in any of the following folders:
C:/Windows/Java/classes/mystuff * C:/Windows/Java/lib/mystuff
C:/mylibrary/mystuff
In addition, most Java virtual machines (VMS) can use class files stored in the form of zip, jar, or cab. However, these zip, jar, or cab files must be explicitly listed in the class path. These compressed files can contain other files or folders with many subfolders.
You can use the following two folders to update the class path:
C:/Windows/Java/classes. Zip
C:/library/myclasses. Cab
Currently, the class file myclass. Class may appear in classes. Zip or myclasses. Cab. If it is included in myclasses. Cab, save it in the sub-Folder mystuff.
Access Control
Three access modifiers related to access: public, private, and protected ).
We have already introduced the fourth access method with no associated keywords: default access (default access); items with default access can only be accessed by items defined in the same software package; because it only assigns access permissions to members in the same software package, it is also called package access ). For example, in the following code, a class and a method are public and both have default access:
Package mystuff;
Public class myclass
{
Public void getmorestuff ()
{
...
}
Void dosomemore ()
{
...
}
}
Class helper
{
}
We can see from the above introduction that the following statements are correct:
* Any class can define member variables of the mystuff. myclass type;
* Methods in any class can define local variables of the mystuff. myclass type;
* If a mystuff. myclass object is specified, the getmorestuff method can be called for methods in any class;
* Only the classes in the mystuff software package can define member variables of the mystuff. helper type;
* Only the methods in the class of the mystuff package can define local variables of the mystuff. helper type;
* If a mystuff. myclass object is specified, the dosomemore method can be called only for methods in the class in the mystuff software package.
With default access or software package access, the software package creator can define classes, methods, and member variables that can only be accessed by other classes in the same software package.
Java software package
Let's quickly browse some software packages that come with Java VM. These packages are used. A standard section of the portable and extended Java library is released. The following describes Java in detail. lang software package, and summarize the other eight software packages.
Java. Lang Software Package
The core part of the Java language is the java. Lang software package, which defines most basic classes in Java. Its core position is obvious: Every Java source code file introduces statements in the Java. Lang class:
Import java. Lang .*;
The Java. lang package contains the object (target) class, the root node of the entire class hierarchy in Java. Lang. object class type java. This package also defines the basic data type class:
String Boolean
Character byte
Integer short
Long float
Double
These classes support numeric conversion and string operations, which are described in detail in the previous section. Other classes defined in Java. Lang are listed in the following table.
Several important classes defined in the Java. Lang Software Package
--------------------------
Java. Lang class description
--------------------------
Class provides support for saving runtime nstanceof operator information
Math provides mathematical constants such as PI and E and trigonometric functions.
System provides access to the operating system, including default I/O streams, environment variables, automatic garbage collection, system time, and system attributes. Many system methods access the runtime class.
Runtime provides access to the operating system. lang. system can access most runtime methods more easily. The only exception is the exec method, which starts a new process thread and Java. lang. runnable interface synergy provides multi-thread support in Java;
Throwable is the base class of all exceptions in Java. It is a superclass of Java. Lang. exception, java. Lang. error, and Java. Lang. runtimeexception. When an exception or error occurs when the application is running, a signal is sent;
Now let's look at some other Java software packages:
Java. Applet --- support for Java Applet and sound editing;
Java. AWT --- Java's inherent window and Drawing Software Package: AWT-Abstract Window Toolkit, which includes basic drawing functions such as fonts, colors, and shapes, it also supports some components, such as buttons, list boxes, menus, text boxes, and layout manager for controlling and arranging components. AWT is portable
Java. Beans --- Java Beans defines the application programming interface (API); Java Beans is a neutral platform component structure in the Java application environment;
Java. Io --- defines a large number of local I/O support classes. It supports both byte stream I/O and batch stream I/O.
Java. Math --- supports mathematical operations on any precision value, including two classes: bigdecimal and biginteger.
Java.net --- supports URL-based and socket-based network I/O;
Java. Text --- provides classes for international applications, including date formats, numbers, currency formats, and sorting order.
Java. util --- includes other classes to support data structures, random numbers, dates, time zones, and calendars.
WFC Software Package
Until now, most of the Code that has appeared uses the Windows base class library (wfclibrary-Windows Foundation Class Library) brought by Visual J ++. All WFC classes are in the com. ms. in the WFC package.
* App --- Contains classes that support applications, including: application, message, window, clipboard ),
Dataformat, registry, time, timer, and version ).
* It is a very important software package. After browsing the main WFC software package, it will take some time to introduce several important classes in this package.
* Core --- the WFC application and view designer define core classes, including component, container, and event) eventhandler, eventinfo, classinfo, and enum. The software package also defines some basic editors used in the view designer.
* Data and data. UI --- Provide database access using ADO (ActiveX Data Objects.
* HTML --- supports hypertext links (HTML-Dynamic HTML ).
* IO --- supports Win32 file I/O.
* OLE32 --- supports drag and drop of Ole.
* UI --- defines the user interface components in WFC. The reader has actually used a lot of classes in this software package: Because classes in the package are used every time a form is used.
* Util --- defines some category classes for the data structure.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service