java-Base-Package-modifier-code block

Source: Internet
Author: User
Tags modifier modifiers

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)

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 ();

Simplified access to Class L

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 the class that we want to use 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

L Import Guide Package code where to write: After declaring the package, define all class classes and use the import package name. Package name. class name;

Access modifiers

Public

Protected

Default

Private

In the same class

In the same package (subclass and unrelated classes)

Sub-classes of different packages

Unrelated classes in different packages

L to be able to access only in this class using the private decoration;

L to think that the classes in this package can be accessed without modifiers;

L to think of classes in this package and subclasses in other packages can be accessed using the protected adornment

I want all the classes in the package to be accessible using the public adornment.

Note: If the class is decorated with public, the class name must be the same as the file name. There can be only one public-decorated class in a file.

Code block local code block:

A local code block is defined in a method or statement

Characteristics:

L The area of code delimited by "{}", at which point you only need to focus on the different scopes

L methods and classes delimit boundaries in a code block way.

class demo{    publicstaticvoid  main (string[] args)    {        {                   int x = 1;                 System.out.println ("Normal code block" + x);        }         int x =;        System.out.println ("Outside the code block" + x);}    }

Results:

Common code block 1

Outside the code block 99

Building code Blocks

Constructing a code block is a block of code that defines the position of a member in a class

Characteristics:

L takes precedence over construction method execution, and constructs code blocks to perform initialization actions required by all objects

Every object that is created executes a block of construction code at a time.

 Public classPerson {PrivateString name; Private intAge ; //Building Code Blocks{System.out.println ("The Construction code block has executed"); } person () {System.out.println ("Person with parameterless constructor execution"); } person (intAge ) {         This. Age =Age ; System.out.println (The constructor for the person parameter executes the); }}classpersondemo{ Public Static voidMain (string[] args) {person P=NewPerson (); Person P1=NewPerson (23); }}
Static code block

A static code block is a block of code that is defined at the member location, using the static adornment.

Characteristics:

It takes precedence over the execution of the Main method, which takes precedence over the construction of code block execution, when used in any form for the first time to the class.

No matter how many objects are created, the static code block executes only once.

L can be used to assign a value to a static variable to initialize the class.

 Public class Person {    private  String name;     Private int Age ;      // Static code block    Static {        System.out.println ("Static code block executed");}    }

java-Base-Package-modifier-code block

Related Article

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.