Modifier in Java (j2se entry 6)

Source: Internet
Author: User

Modifiers in Java

StaticStatic. It can modify attributes, methods, and code blocks.

1. Static ModificationAttribute (class variable)This attribute can be accessed by class name. attribute name, that is, to make this attribute a class variable of this class, which is shared by this class object. This attribute is fully public. (The common class variables are irrelevant to the object and only related to the class ).

During the class loading process, the class itself is also stored in the file (the bytecode file stores the class information), Java will use the I/O Stream to put the class file (the bytecode file) read Into JVM (Java Virtual Machine). This process becomes the loading of classes. the JVM (Java Virtual Machine) uses the classpath to find the bytecode file..

Class variables are automatically initialized during loading. The initialization rules are the same as the instance variables..

Note:The instance variables in the class are initialized when the object is created, and the static modified attributes, that is, class variables, are created and initialized when the class is loaded, class loading is performed once. That is, the class variable will only be created once..

2. Static ModificationMethod (static method)This method becomes the public method of the entire class and can be accessed by class name. method name.

Note: static modification methods cannot be directly accessed (can be accessed in combination). Non-static (static) members in this class (including methods and attributes ), the non-static (static) method of this class can access static members (including methods and attributes) of this class, and can call static methods. Use static methods with caution. This keyword cannot appear in static methods.

Note: The parent class is a static method, and the subclass cannot cover non-static methods. In the Parent and Child classes, static Methods in the parent class can be overwritten by static methods in the quilt class, but there is no polymorphism. (When using objects to call static methods, they are actually static methods of the compile type)

Note: In the Parent and Child classes,Static methods can only be overwritten by static methods. Non-static methods can only be overwritten by non-static methods..
The main method in Java must be written as static because objects cannot be created during class loading, because static methods can be called without objects, so when the main method of the class is loaded, you can run the program through the main method entry..

Note: In combination mode, you need to create a required object in the method, and use this object to call the content of any desired object, without being limited by access to static resources.

3. Static ModificationInitial code blockThe initial code block is calledStatic initial code block, which is executed only once during class loading. You can use a static initial code block to initialize a class..

Dynamic initial code block, Written in "{}" in the class body. This code block is run in the initialization attribute of the generated object. This type of code block is called a dynamic initial code block.

When the class will be loaded and the class will be loaded when the object is constructed (created). Calling static methods in the class or accessing static properties will also load the class where the static method is actually located. When constructing a subclass object, the parent class must be loaded first. The class loading has a delay loading principle, which is only loaded when it is required..

FinalModifier, which can be used to modify variables, methods, and classes.

1,Final modifier variable
Modified by fianlVariableIt will becomeConstant(Constants should be capitalized ),Once assigned, it cannot be changed., (Values can be assigned directly during initialization, or in the constructor. You can only select either of the two methods, but cannot assign values to constants ),Fianl constants do not have default values.The final modifier is often used together with the static modifier when the initialization is a value assignment.

2, finalModifier, The method modified by final willIt cannot be overwritten by its subclass, and the method stability cannot be overwritten..
3, finalModifier, The class modified by final willCannot be inherited. Methods In the final class are also final.

Note:Final,Cannot be used to modify the constructor.If a constant attribute exists in the parent class, the class of the parent class is not loaded when the constant attribute is used in the subclass. If the value of a static constant can be determined, the class will not be loaded. If it cannot be determined, the class where the constant is located will be loaded.

Unchanged mode: once an object is created, its attributes do not change. Use final to modify attributes, final to modify classes (strong immutable mode), and final to modify attributes (weak immutable mode ).
Typical Embodiment of the unchanged mode: Java. Lang. string class,In the constant mode, objects can be shared (an object instance can be used to assign values to multiple object variables).

The pooling idea is to place the data to be shared in the pool (space-saving and data-sharing). Only the string class can use the literal value in "" to create an object. When a string class is created with a literal value, it is searched in the string pool space of the Java method space. If yes, the address of the string in the string pool is returned, and pay the address to the object variable. If not, a string object is created in the string pool and its address is returned to pay for the object variable. When another object is created with the nominal value, the above process is repeated.
If it is a new object that creates a string class in the heap space, there will be no such process (the reason for the low efficiency of string connection ).


The intern () method in the string class will compare the string in the string class object created in the heap space with the string in the string pool, if the same string exists, the address in the string pool of the string is returned.

The constant mode is used to modify objects. Adding operations is quite troublesome, and many intermediate junk objects are generated. The overhead for creating and destroying resources is quite large.
The efficiency of the string class before string connection is very low, because the attributes of the objects it produces cannot be modified. When the string is connected, only new objects can be created.

For many string connections, useStringbufferClass, no redundant intermediate object is generated when the class object is used for string connection, thus optimizing the efficiency.

Abstract(Abstract) modifier, which can be used to modify classes and Methods

1,Abstract ModifierThis class will become an abstract class. This class will not generate an object instance, but it can be used as the type of object variable Declaration, that is, the compile-time type. abstract classes are like a class of semi-finished products, the subclass must inherit and overwrite the abstract methods.

2,Abstract modification methodThis method will be converted into an abstract method, that is, it can only be declared (defined) but not implemented. The implementation part will be replaced. Subclass inheritance is required (overwrite ).

Note:Classes with abstract methods must be abstract classes. However, not all abstract methods are abstract methods, but also specific methods.

The abstract modifier must be placed before the class name.

Abstract class testabs {

}

Abstract modification requires that its subclass overwrite (implement. When calling, you can call the method after the subclass overwrites (implement) in a multi-state manner, that is, the abstract method must be implemented in its subclass, unless the subclass itself is also an abstract class.

Note: If the parent class is an abstract class with an abstract method, the subclass inherits the parent class and implements (overwrites) All the abstract methods in the parent class, A subclass can create an instance of an object. Otherwise, the subclass must be an abstract class. Abstract classes can have constructor methods, which are the constructor of the parent class (abstract class) that a subclass needs to call when constructing a subclass object.

Final, abstract, Private, abstract, static, and abstract are modifiers that cannot be put together.BecauseAbstract modifier is called in a multi-state manner only when it is implemented (overwritten) in its subclass. The above modifier does not cover this method during the modifier period, final cannot be overwritten, private cannot be inherited to sub-classes, so it cannot be overwritten, and static can be overwritten. However, a method of the type during compilation will be called during the call, because the methods of the parent class are called, and the methods of the parent class are abstract methods and cannot be called, The modifiers on cannot be put together..

Abstract METHODS represent certain standards, defining standards, defining functions, and implementing functions in sub-classes.(Subclass inherits the parent class and must provide the Implementation of abstract methods inherited from the parent class ).
Methods can be implemented unexpectedly at a time, or define certain standards for subclass implementation. This method can be defined as abstract. (Abstract)

Template Method mode:Abstract is used to separate standards for formulation and implementation. The standard is template, and implementation is implemented according to template standards.That is, the method that inherits the template and implements the corresponding functions in the template. Methods that are not allowed to be modified in the template can be modified using fianl. This method cannot be used as an abstract method. To ensure security and encapsulation, you can use protected to modify the non-public parts of the template.

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.