Package definition and import, system commonly used packages

Source: Internet
Author: User
Tags mul

Definition of package

The package is a measure to avoid duplicate names when using multiple classes and interfaces, simply by adding the Packages keyword to the program.

Package Definition Format:

Package name. Child package name;

 package org.lxh.demo08;    // Define a package class demo{    public  String getInfo () {        return ' Hello world!!! '  ;    }};  Public class packagedemo01{    publicstaticvoid  main (String args[]) {        System.out.println (new  Demo (). GetInfo ());}    ;

After the package is defined, in fact, the class name is: Package. Class name.

The so-called package is actually a folder, a *.class file to be saved in a folder.

Import of packages

Import statement:

When a package's class file requires the use of another package's class file, the import directive is required.

Demo.java:

 Package org.lxh.demo08.a; class demo{    public  String getcontent () {        return "Mldn lxh" ;    }};

Importdemo.java;

 package org.lxh.demo08.b;    // put it in a different package. import org.lxh.demo08.a.*;    // Import the demo class from different packages  Public class importdemo01{    publicstaticvoid  main (String args[]) {        System.out.println (new  Demo (). GetInfo ());}    ;

When compiling, you should compile the Demo.java class before compiling Importdemo.java, because the latter uses the former class.

The following error occurred in compiling:

Exception in thread "main" java.lang.Error:Unresolved compilation Problem: The     type Demo isn't visible at    or G.lxh.demo08.b.importdemo01.main (Importdemo01.java:7)

That is, the demo class is not visible because the access type of the demo class is not public, so modify the demo class to be of the public type.

 Package org.lxh.demo08.a;  Public class Demo {    public  String getInfo () {        return ' Hello world!!! '  ;    }}

Operation Result:

Hello World!!!
class and public class complete declaration

When a class is declared as public class, the file name must be the same as the class name, and there can be only one public class in a class, and if you declare a class using class, the class name can be

The file name is inconsistent, but the generated class file must be executed at execution time.

if a class is accessed only in this package and does not need to be outsourced, it is declared directly as class, and if a class needs to be outsourced, it must be declared as public class.

Suppose you want to import many classes in a package and use "*" directly.

System common Packages

new Java Features-static import

If a class is all static methods declared with static, you can import it directly using the "import static" format when importing

The import format is as follows: Import static package. Class. *;

Call inside the method that can be called directly, as follows:

 Packageorg.lxh.demo08.e; Public classoperate{//all the methods inside are static types.     Public Static intAddintIintj) {//addition Operation        returni +J; }     Public Static intSubintIintj) {//Subtraction Operation        returnIJ; }     Public Static intMulintIintj) {//multiplication Operation        returnIJ; }     Public Static intDivintIintj) {//Division Operation        returnIJ; }};

Use static Import Mode: import static package. Class. *;

 Packageorg.lxh.demo08.f;import static org.lxh.demo08.e.operate.*; Static Import Public classstaticimportdemo{ Public Static voidMain (String args[]) {System.out.println ("3 + 3 =" +Add (3,3)) ;//Call static methods directlySystem.out.println ("3-2 =" +Sub (3,2)) ;//Call static methods directlySYSTEM.OUT.PRINTLN ("3 * 3 =" +Mul (3,3)) ;//Call static methods directlySystem.out.println ("3/3 =" +Div (3,3)) ;//calling static methods directly    }};
Jar command

Summarize

Package definition and import, system commonly used packages

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.