Docsity

packages in java language, Study notes of Law

Partial preview of the text

Download packages in java language and more Study notes Law in PDF only on Docsity!

Chapter 7 – Packages – (unknown facts)

For convenience, the Java compiler automatically imports two entire packages for each source

file: (1) the java.lang package and (2) the current package (the package for the current file).

(java. lang package is a default package in Java therefore, there is no need to import it

explicitly. i.e. without importing you can access the classes of this package.)

Members of a pkg –

1. Classes

2. Interfaces

3. Subpackages

A package consists of a number of compilation units (§7.3). A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang. import declarations (§7.5) that allow types from other packages and static members of types to be referred to using their simple names. A compilation unit consists of three parts, each of which is optional:

 A package declaration (§7.4), giving the fully qualified name (§6.7) of the package to which the compilation

unit belongs.

A compilation unit that has no package declaration is part of an unnamed package (§7.4.2).

 import declarations (§7.5) that allow types from other packages and static members of types to be

referred to using their simple names.

 Top level type declarations (§7.6) of class and interface types.

 Every compilation unit implicitly imports every public type name declared in the predefined

package java.lang, as if the declaration import java.lang.*; appeared at the beginning of each

compilation unit immediately after any package statement. As a result, the names of all those types are

available as simple names in every compilation unit.

 All the compilation units of the predefined package java and its subpackages lang and io are

always observable.

7.4. Package Declarations

7.4.1. Named Packages

{Annotation} package Identifier {. Identifier} ; In the absence of an access modifier, a top level type has package access: it is accessible only within compilation units of the package in which it is declared (§6.6.1). A type may be declared public to grant access to the type from code in other packages.

Here is the fully qualified name for the Rectangle class declared in the graphics package

graphics.Rectangle

You could use this qualified name to create an instance of graphics.Rectangle:

graphics.Rectangle myRect = new graphics.Rectangle();

 Import the package member

Here's how you would import the Rectangle class from the graphics package

import graphics.Rectangle;

Now you can refer to the Rectangle class by its simple name.

Rectangle myRectangle = new Rectangle();

 Import the member's entire package

(Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or

a static member of another type, is to use a fully qualified name (§6.7).)

ImportDeclaration: SingleTypeImportDeclaration TypeImportOnDemandDeclaration SingleStaticImportDeclaration StaticImportOnDemandDeclaration

 A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).

 A type-import-on-demand declaration (§7.5.2) imports all the accessible types (§6.6) of a named type or named

package as needed, by mentioning the canonical name of a type or package.

 A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a

type, by giving its canonical name.

 A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as

needed, by mentioning the canonical name of a type. The scope and shadowing of a type or member imported by these declarations is specified in §6.3 and §6.4.

An import declaration makes types or members available by their simple names only within the compilation unit that actually contains

the import declaration. The scope of the type(s) or member(s) introduced by an import declaration specifically does not include

other compilation units in the same package, other import declarations in the current compilation unit, or a package declaration in

the current compilation unit (except for the annotations of a package declaration).

7.5.1. Single-Type-Import Declarations

SingleTypeImportDeclaration:

import TypeName ;

The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type

1.5.2. Type-Import-on-Demand Declarations

TypeImportOnDemandDeclaration:

import PackageOrTypeName. * ;

The PackageOrTypeName must be the canonical name (§6.7) of a package, a class type, an interface type, an enum type, or an annotation type.

1.5.3. Single-Static-Import Declarations

There are situations where you need frequent access to static final fields (constants) and static

methods from one or two classes. Prefixing the name of these classes over and over can result in

cluttered code. The static import statement gives you a way to import the constants and static methods

that you want to use so that you do not need to prefix the name of their class.

SingleStaticImportDeclaration:

import static TypeName. Identifier ;

(A single-static-import declaration imports all accessible static members with a given simple name from a type. This makes these static members available under their simple name in the class and interface declarations of the compilation unit in which the single-static-import declaration appears.) The TypeName must be the canonical name (§6.7) of a class type, interface type, enum type, or annotation type.

This is used when you want to import any static member from a package. You use this when

you want to import any constant or any static method in the API.

Example : in https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html;

import static java.lang.Math. PI ;

import static java.lang.String. copyValueOf ;