The eighth chapter object-oriented (II.)

Source: Internet
Author: User

8.1 static (Static) keyword
Used to decorate member variables and member functions
8.1.1 characteristics of members with static modifications

      • Loads as the class is loaded and disappears as the class disappears

      • Precedence over the existence of objects

      • shared by all objects

      • Can be called directly by the class name (class name. Static members)

      • Store in a shared area (method area (method of Class), data area)

8.1.2 Use

      • Static methods can only call static members

      • The This, super keyword cannot be written in a static method

8.2 Static code block

8.2.1 Format

static{

execute the statement;

}

8.2.2 Features

Executes as the class loads, takes precedence over the main function, and executes only once.

8.2.3 Effect

Used to initialize a class.

Note: Static members can only be accessed in static code blocks, and non-static members cannot be accessed

8.3 Design Patterns

The most effective way to solve a certain type of problem.

There are 23 design patterns in Java.

8.3.1 Single-Case mode

Resolves a class that only has one object in memory

8.3.1.1 thought

To ensure that the object is unique:

    1. In order to avoid creating objects with too many programs, the program is first prevented from establishing such objects.

    2. It is also necessary to customize an object in this class in order for the program to have access to the class object.

    3. To facilitate the program's access to custom objects, provide some way to access them.

8.3.1.2 Concrete Implementation

    1. Privatize the constructor. (This will not allow the program to create such objects.)

    2. Creates an object of this class in a class.

    3. Provides a way to get to the object.


Initialize the object first, called: a Hungry man type

CALSS single{

Private single () {}

private static Single S = new single ();

public static single getinstance () {

return s;

}

}

The object is initialized when the method is called, or deferred loading of the object, called: Lazy-Dog

CALSS single{

private static single s = null;

Private single () {}

public static single getinstance () {

if (s==null) {

s = new single ();

}

return s;

}

}

Call: Single.getinstance ();

This article is from the "Java Basics" blog, so be sure to keep this source http://8163413.blog.51cto.com/8153413/1690629

The eighth chapter object-oriented (II.)

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.