Definition of Java Domain __java

Source: Internet
Author: User
Tags modifier volatile
DomainAlso called the member variable, embodies the state of the class, and the change of State is realized by the method of the class body. Definition of a domain: Defines a static property of a class. fields must be defined directly in the class body and cannot be included in the method definition. The format of the field definition is as follows: [modifier] type name Domain name; Note: The modifier part is optional and can be one or more combinations of keywords public, protected, private, final, static, transient, and volatile; Public , protected and private represent the access rights of the domain;      Final representation field is constant and cannot be changed;      The static description defines a variable that is statically variable;      transient indicates that the variable is a temporary variable;      volatile-decorated variables are used in multithreaded programs to illustrate backup variables. static fields:Fields that are modified by static become static domain (class domain, class variable)。 For example: static int n = 18; A static domain is a member variable belonging to the entire class, stored in a common storage unit in the memory area of the class. (That is, any object of that class can access it, and access to it gets the same value, and if an object modifies its value, the value of all objects will change together.) Note: Static fields, static fields, static fields that are modified with static are not confused with other domains! Domains that are not static-decorated have separate memory addresses in different objects, meaning that each object's domain value is not the same. Be sure to distinguish it from static fields. Static fields can be accessed directly by using the class name, or they can be referenced by objects.
Initialization of static classes
A static class does not belong to a specific object, but a domain of the entire class, so its initialization is implemented through a static initializer. A static initializer is a compound statement consisting of a keyword static and a pair of curly braces, intended to initialize a class domain. Static initialization occurs when the owning class is loaded into memory.
the difference between the initializer and the constructor method of a static class:Static initializers can initialize only static domains, and they cannot access non-static domains. Static initializers consist of keyword static and compound statements, and the initial values of static classes are determined by the program; The construction method is a special method, it has parameters, when the program runs, it can receive data from the outside and initialize the domain to different values. The static initializer initializes the class itself, and the constructor initializes the object. The static initializer is automatically invoked by the system when the class is loaded into memory, and the construction method is implemented automatically by the system when the object is created with the keyword new, and the constructor is followed by the keyword new.
Final domain: The fields that are decorated with the keyword final are called the final domain. The value of the final decorated field will not change during program execution. (Final modified variables are also so once assigned can not be changed) Example: PI pi defined in the "Java.lang" package is a final domain. Can be expressed as "Math.PI" NOTE: Because the value of the final field is fixed, in order to save space, the final field is generally static decorated, as the final domain. Another:     The final modified method is the ultimate method: The function of the method is determined, can not cover the same method in the quilt class, this can guarantee the consistency of the method implementation.     Final Modified class: This class is the final class, cannot derive the subclass, its content, the attribute and the method realizes the fixed invariant, and the class name forms the stable mapping relation, guarantees the function realization accuracy.         Common final classes: for example: System class and InetAddress class, Socket class, etc.

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.