[Package and access control permission] _ package definition and import notes

Source: Internet
Author: User

Objectives of this chapter:
Measure the test taker's understanding about the development of multiple developers.
Measure the test taker's knowledge about the role and definition of the package.
Understand the package import syntax and restrictions
Understand common system packages
Master new Java features-static Import
Understanding how to use jar commands

Large-scale Program Development

If multiple developers develop the same project, the same class name will certainly occur. In this way, it will be more troublesome.
The same file will be overwritten.

Package definition:

Package package name. Sub-package name;

Package org. lxh. demo08; // defines a package class demo {Public String getinfo () {return "Hello world !!! ";}} Public class packagedemo01 {system. Out. println (new demo (). getinfo ());}

After the package is defined, the actual class name is: package. Class Name.

Package and compile the program:

Javac-D. packagedemo01.java
After generating *. class, you can directly access it.

Java org. lxh. demo08.packagedemo01

Directly package and compile multiple java files
Javac-D. *. Java

3.3 package Import

When the class file of a class package needs to use the class file of another package, the import command must be used. \

Import Statement

Syntax:
Import package name. Sub-package name. Class Name;-> manually import the required class
Import package name. Sub-package name. *;-> the required class automatically loaded by JVM

Demo. Java

public org.lxh.demo08.a;public class Demo{    public String getInfo(){        return "Hello World!!!";        }}

Package Org. lxh. demo08. B; // put it in different packages import Org. lxh. demo08.a. demo; // import different demo classes public class importdemo01 {public static void main (string ARGs []) {system. out. println (new demo (). getinfo ());}}

During compilation, you should first compile the demo. Java class, and then compile importdemo01.java, because the latter uses the class of the former for operations.

Complete supplement to public Class and Class declaration classes

If a class is declared as public class, the file name must be consistent with the class name, and only one public class can be included in a class. If a class is declared as a class, the file name may be different from the class name, but the generated class file name must be executed during execution. In addition, the Public Class and Class are restricted in Packet Access. If a class is only accessed in this package and does not need to be accessed by outsourcing, it can be directly declared as a class, if a class needs to be accessed by outsourcing, it must be declared as public class.

If you want to import many classes in a package, it will be very troublesome to write. You can directly import data using the "*" method.

Import org. lxh. demo08.a. Demo;
Import org. lxh. demo08.a .*;
Both have the same performance.

Q: What should I do when two packages with the same class name are imported at the same time?

package org.lxh.demo08.d;import    org.lxh.demo08.a.*;import    org.lxh.demo08.c.*;public class ImportDemo02{    public static void main(String args[]){        org.lxh.demo08.a.Demo d = new org.lxh.demo08.a.Demo();        System.out.println(d.getInfo());    }}

3.4 common system packages
No package name
1 java. Lang this package is a basic package, and classes such as string are saved in this package.
2 Java. Lang. reflect this package is a reflection mechanism package. The sub-package of Java. Lang will be introduced to readers in common Java class libraries.
3 java. util this package is a toolkit, and some common class libraries, date operations, and so on are all in this class. If you master this package, it is clear that you are proficient in all kinds of design ideas.
4. java. text provides text processing classes.
5 java. SQL database operation class, which provides various database operation classes and interfaces
6. Complete network programming using java.net
7. java. Io Input/Output Processing
8 java. AWT contains multiple classes of Abstract Window toolkits, which are used to build and manage graphical interfaces of applications.
9 java. Swing this package is used to build a graphical user interface. This package is lightweight than Java. AWT.

Java. util. arrays. Sort ()
Of course, there will also be a large number of sub-packages in these packages.

3.5 static Import

Import format: Import static package. Class .*;
Instance:

package org.lxh.demo08.e;public class Operate{    public static int add(int i,int j){        return i+j;    }    public static int sub(int i,int j){        return i-j;        }    public static int mul(int i,int j){        return i*j;    }    public static int div(int i,int j){        return i/j;    }}
Package Org. lxh. demo08.f; import static Org. lxh. demo08.e. operate. *; // static import public class staticimportdemo {public static void main (string ARGs []) {system. out. println ("3 + 3 =" + Add (3, 3); system. out. println ("3-3 =" + sub (3, 3); system. out. println ("3*3 =" + MUL (3, 3); system. out. println ("3/3 =" + Div ));}}

3.6. Jar command
Compress the *. Class file into a file for use by the customer. Such a file is called a jar file.

Main Parameters in the jar command:
"C": Create a new document
"V": generate detailed input information
"F": Specifies an existing document name.

package org.lxh.demo08.demo;public class Hello{    public String getInfo(){        return "Hello World!!!";    }}

Package and compile it: javac-D. Hello. Java
Compress it into a jar package: jar-CVF my. Jar org

To use the classes in the jar package, you must configure the classpath path.
Set classpath =.; D: \ packdemo \ My. Jar

Use classes in jar

package org.lxh.demo08;import org.lxh.demo08.demo.Hello;public class ImportJarDemo{    public static void main(String args[]){        Hello hello = new Hello();        System.out.println(hello.getInfo());        }}

In actual Java Development, some practical tool classes are often packaged into jar packages for users.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.