2.24 Java Basics Summary ① Access ②static③ static method ④ instance initialization block ⑤ static initialization block

Source: Internet
Author: User

① access rights ②static③ static method ④ instance initialization block ⑤ static initialization block
First, access rights

The default is the same package
Protected are related, that is, the subclasses of the same package and different packages
Private only this class, public all

This class Same package Different packages
Sub-class Non-sub-class Sub-class Non-sub-class
Private
Default (same package)
Protected
Public

Second, static
Static member variables, class level, all classes share a
Person.country = "China";
Generally used when there are common attributes, or constants
public static final int pi = 3.14; otherwise each object will have a pi at new and a waste of space
You can not create an object direct transfer, or you can new an object in the call
Features ① Object-independent, class-dependent ② all classes share a value ③ preload (loaded as the class is loaded)

public static int num = 0;
Construct () {
num++;
}

Third, static method
Loaded as the class is loaded
The static method cannot manipulate any properties and methods that are not static.
Reason: ① class level, Object-independent ② class is loaded first, non-static is not loaded into memory

Common in: tool methods for tools

Iv. Instance initialization block
{}
Construct () {}
Automatically executes when the object is generated, new executes several times, uses less, because it can be written to the first few lines of this class of construction method
After the construction method of the parent class, before the constructor method of this class

V. Static initialization blocks
static{}
The class is loaded automatically and executes only once to make the usage large
For example, in some preparatory work, file preparation, database initialization

New sub-class ();

Order of execution: Fu Jing, Zi Jing, Fu Real, Fu, fruiting, sub-structure
Parent class static initialization block, subclass static initialization block (first object, new subclass will first tune parent class)
The parent class instance initializes the block, the parent class constructor (the tone class constructs the parent class construct first, the instance initializes the block before the constructor)
Subclass instance initialization block, subclass constructor

2.24 Java Basics Summary ① Access ②static③ static method ④ instance initialization block ⑤ static initialization 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.