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:
In order to avoid creating objects with too many programs, the program is first prevented from establishing such objects.
It is also necessary to customize an object in this class in order for the program to have access to the class object.
To facilitate the program's access to custom objects, provide some way to access them.
8.3.1.2 Concrete Implementation
Privatize the constructor. (This will not allow the program to create such objects.)
Creates an object of this class in a class.
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.)