Features and usage of static (static) keywords in PHP

Source: Internet
Author: User
→ Static: statics

Usage: is a modifier used to decorate members (member variables, member functions)

Class person{    String country = "cn";}

/* Each instantiation of a person object, each object has a country in the heap memory

In addition to static, there is only one static country in memory that is called directly by other objects, which saves memory space.

When a member is statically decorated, it can be called directly by the class name in addition to being called by the object. */

Unique content as objects are stored

Like the person class, each Chinese has its own name, and the nationality is the same in China, so contry can be defined as static and name is not used.

→static Features

1. Load with class loading

That is, the static disappears as the class disappears, which means that his life cycle is the longest.

2. Precedence over object existence

3. Shared by all objects

4, can be directly called by the class name

→int the difference between an age (member variable) instance variable and a static int age (static member variable) class variable

1, storage for

Instance variables are present in heap memory as objects are created

Class variables exist in the method area as the class is loaded

2. Life cycle

Instance variables disappear as the object disappears

The class variable has the longest life cycle and disappears as the class disappears

→ Static Use precautions

1. Static methods can only access static members

2. Cannot define This,super keyword in static method

Because static takes precedence over the existence of an object, this is not available in a static method

3. The main function is static

→ Static Benefits and disadvantages

Benefit: Save space by storing the object's data in a separate space, without the need to store one copy of each object

Can be called directly by the class name (Person.country)

Cons: The life cycle is too long.

The limitations of access occur. (only static access)

→ When do I use static?

Two things to start with

Because statically decorated content has member variables and member methods (functions)

When do you define static variables (class variables)?

When shared data is present in an object, the data is statically decorated

The unique data in the object to be defined as non-static exists in heap memory.

When do you define static functions?

This function can be defined as static when there is no access to non-static data (unique data for the object) within the function.

→ Static Applications

Each application has a common function

These functions can be extracted, individually encapsulated

For reuse.

The member method is defined as static directly called by the class name.

There is a default constructor in a class, and the permissions of this constructor are consistent with the classes that belong to it

Static code block

Format:

Static

{

Execution statements in a static code block;

}

Feature: Executes only once when the class is loaded. Used to initialize a class.

Here's an example:

Class staticcode{    static     {        system,out.println ("a");}    }

Load order static code block--Construct code block--constructor

Initialization of an object

Person p = new person ("Zhangsan", 20);

1, because new uses the Person.class. Therefore, the Person.class file is found and loaded into memory first.

2. Execute the static block of code in the class (block), and, if any, initialize the Person.class class

3. Open space in heap memory, not with memory address

4. Establish unique properties of the object in heap memory and initialize it by default

5, the attribute is displayed initialization

6. Construct code block initialization for object

7. Initialize the object with the corresponding constructor function.

8. Pay the memory address to the P variable in the stack memory

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.