Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

packages in java language, Study notes of Law

the package information in java language and all of it

Typology: Study notes

2021/2022

Uploaded on 01/18/2023

kalanipr789
kalanipr789 🇱🇰

5 documents

1 / 6

Toggle sidebar

Related documents


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. 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 ; Obviously, you can write your own classes that contain constants and static methods that you use frequently, and then use the static import statement. For example, import static mypackage.MyConstants.*; 7.5.4. Static-Import-on-Demand Declarations StaticImportOnDemandDeclaration: import static TypeName . * ; A static-import-on-demand declaration allows all accessible static members of a named type to be imported as needed. The TypeName must be the canonical name (§6.7) of a class type, interface type, enum type, or annotation type. 7.6. Top Level Type Declarations
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved