Static retouching of Java notes and a single-case design pattern

Source: Internet
Author: User
Tags modifiers


Sixth day notes

Static modifiers static:

a . Static Modifying member Variables :

    1. Static is used to modify a member variable to be called a statically member variable, and a member variable without a static modifier is called a non-static member variable
    2. How static members are Accessed:

(1) access by object: name of the OBJECT. variable name

(2) Accessing by Class Name: class Name. variable name;

    1. Use note points:

(1) static modification is required only if data sharing is required

Public properties
String name;
This variable becomes a shared data, stored separately in one place
The static String country;//variable is initialized only once. There is one in the In-memory Value.

(2) only static variables can be called with the class name, non-static variables can only be called through the object

(3) Local variables are not allowed to be modified by static

two . Static used to modify the method

1. Access method:

(1) access by object: name of the Object. method name

(2) Accessing by Class Name: class Name. method name; Recommended Use

public static void Eat () {

System.out.println (country+ "is eating");
}

2. Use note points:

(1) static methods can be called with objects and class Names. a non-static method can only be called with an object.

(2) A static method is a static member variable that can be accessed without access to a Non-static member variable, and any member variable can be accessed in a non-static method.

Cause: It is possible that the static method is called, but the object has not been created Yet.

(3) static methods can access static methods directly, but not non-static methods. a non-static method is a static method that can be Accessed.

Cause: It is possible that the static method is called, but the object has not been created Yet.

(4) the This keyword and the Super keyword are not allowed in static methods

3. When static is used to modify the Method:

When a method does not need to directly access non-static data, This time can be modified with static (define tool Class)

4. Features: static data is older than non-static data exists in Memory.

three . Static code block :

The compiled Java.class file is executed when it is Used.

Permission Modifiers :

    1. Public: common, Open permissions are the largest and any can be invoked under any circumstances
    2. Private: the privilege is minimal and can only be accessed by itself in this class
    3. Protected: Protected.
    4. Why does the main function define This:

(1) Why the Public:main Main method is called by the jvm, and the JVM is actually a program, in order to ensure that the JVM can invoke the main function in any Situation.

(2) Static: If there is no static call the Main method is called through the object, the JVM if the creation of objects do not know what parameters to pass so as an example to avoid trouble, the method is defined as static, this time there is no need to consider the object, the direct name of the class can be Called.

(3) Void:main is called by the jvm, the return value is given to the jvm, and the JVM receives no meaning from the return value, so it does not need to return a Value.

(4) main: not the keyword, main and the Convention well, This is called the Entrance.

(5) Args: used to receive parameters passed in while the program is Running.

single case design mode :

1. Singleton design pattern: A class can only create an object.

2. Single-instance Setup Steps:

(1) create an object set him as a private static member variable, guaranteeing that the object is unique

private static Class Name Object name = new class name ();

(2) The construction method is set to private, preventing the new object from being Used.

Private class name () {}

(3) defines a public static method that returns a value of the above Object.

public static return value type GetInstance () {

Return Object name;

}

3.2 Singleton modes: a hungry man single-case mode and lazy single-case mode.

Static retouching of Java notes and a single-case design pattern

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.