Claims and access for packages
concept of the package
Java package, in fact, is the folder in our computer system, the package is stored in the class file.
when the class file is very large, we usually use multiple packages to store and manage them, which is called subcontracting management.
in the project, we put classes of the same functionality into a package for easy administration. And the division of the Daily project is also with the package as border.
the package declared in the class must be consistent with the folder in which the actual class file resides, that is, if the class is declared under a package, the resulting. class file must be under the A folder , or the program will not find the class when it runs.
declaration format for Packages
usually use the company URL anti-write, you can have multi-layer package, the package name in all lowercase letters, multi-layer package between the "." Connection
the declaration format of a package in a class:
Package name. The package name ... ;
such as: Oracle URL oracle.com then the URL is com.oracle
NOTE: Statements that declare packages must be written in the first line of the program's valid code (comments are not counted)
Code Demo:
package com.oracle;//packet declaration, must be in the first line of the valid code
Import Java.util.Scanner;
Import Java.util.Random;
Public class Demo {}
access to the package
When you access a class, you must use the full name of the class containing the package name (the package name, class name)in order to be able to find the class.
Package name. Package name ... . class name
such as: Java.util.Scanner
Java.util.Random
Com.oracle.Demo
class with package, create Object Format: Package name. class Name Variable name = new package name. Class name ();
Com.oracle.Demo d = new Com.oracle.Demo ();
premise: The access to the package is closely related to access rights, in the general case, that is, the class is decorated with public.
Simplified access to classes
When we want to use a class, this class is in the same package as the current program (that is, the same folder), or if the class is a class in the Java.lang package, you can usually omit the name of the swap and use the class directly.
such as: The Com.oracle package has two classes, the Persontest class, and the person class. In the Persontest class, when accessing the person class, because it is under the same package, the package name can be omitted during access, that is, the person is accessed directly from the class name.
Class Name Variable name = new class name ();
Person p = new person ();
When we want to use a class that is not in the same package as the current program (that is, in a different folder), the class to be accessed must be decorated with public to be accessible.
Package com.oracle02;
public class Person {}
Import Guide Package
Each time we use a class, we need to write a long package name. Very troublesome, we can simplify by the way of import guide package .
This class can be used in the form of a guide, which avoids the use of the full class name (that is, the package class. Class name).
Format of the Guide package:
Import package name. class name;
When the program imports the specified package, it can be simplified when the class is used. Demonstrates the following
//before importing a package
//Create object
java.util.random r1 = new Java.util.Random ();
java.util.random r2 = new Java.util.Random ();
java.util.scanner sc1 = new Java.util.Scanner (system.in );
java.util.scanner sc2 = new Java.util.Scanner (system.in );
How the package is imported
Import Java.util.Random;
Import Java.util.Scanner;
Creating objects
Random r1 = new Random ();
Random r2 = new Random ();
Scanner SC1 = new Scanner (system.in);
Scanner SC2 = new Scanner (system.in);
Where the import Guide package code is written: After declaring the package, define all class classes and use the import package name. Package name. class name;
Claims and access to packages in Java