Java Experience---Single case class, final modifier, abstract class __java

Source: Internet
Author: User
Tags constant modifier

1. Design pattern: A problem-solving idea. The most effective way to solve a certain kind of problem. Java has 23 design patterns (summed up by predecessors).

Single-Example design pattern: Resolves a class that has only one object in memory.

if a class can always create only one instance, the class is called a singleton (Singleton) class.

2. Want to make sure the object is unique (three steps to create a single instance Class):

1. Prohibit other programs from creating objects freely: Privatize the construction method and use private decoration.

2. In order for other programs to access the class object, you can define only one object in this class: Create a Class object in the class.

3. To facilitate access to the object by other programs, you can provide an external access method: Provides a public method to get the object.

3. Under normal circumstances, the constructor of a class is defined as public access, which allows any class to create the object freely, but at some point it makes no sense to allow other classes to freely create objects of that class, but it may cause system performance degradation (because each object created consumes a certain amount of system resources).

The provided public method must be decorated with a static (because the object is not present before the method is invoked and is an instance object, there can be only one object that cannot be new, so it is not possible to invoke the method as an object, only a class), and the class must cache objects that have already been created. Otherwise, it will not be possible to know if an object has ever been created, and there is no guarantee that only one object is created, for which the class needs to use a property to hold the object that was created, and because the property needs to be accessed by the static method above, the static decoration must be used.

42 kinds of writing:

1. A hungry man: whether you use it or not, start by creating a single Instance object that initializes the object first.

the development of the general use of this wording, high security

private static single S=newsingle ();

Private single () {}

Public static singlegetinstance () {

return s;


2. Lazy type: When used to establish objects, is delayed loading.

private static single1s=null;

Private Single1 () {}

Public static Single1 getinstance () {

if (s==null) {

s=new Single1 ();}

return s;


5. The final keyword can be used to modify classes, variables, and methods to indicate that the classes, variables, and methods that it modifies are not immutable.

when the final modifier variable, it means that the variable can not be changed once the initial value is obtained, the final can modify both the member variable (including the class and instance variables), or the local variable and the formal parameter.

6.1). Final modified member variable:

class variables: When the class is initialized, the system allocates memory for the class variable and assigns a default value.

instance variable: When an object is created, the system allocates memory for the object's instance property and assigns a default value.

2. The final decorated class properties, instance properties can specify the initial value of the following places:

Class Property: Specifies the initial value when statically initializing a block or declaring the property.

Instance property: A non-static initialization block, declaring the property, or specifying an initial value in the constructor method.

Note: An instance property cannot specify an initial value in a static initialization block. Because the static initialization block is a static member, an instance property-Non-static member is not accessible, the Class property cannot specify an initial value in the normal initialization block because the class property is initialized at the class initialization stage and the normal initialization block cannot be assigned a value.

for final-decorated member variables, they can no longer be assignable once they have an initial value, so they cannot be assignable in the normal method, which can only specify a default value (display Assignment) when defining it, or in a static initialization block, Initialization blocks and constructors specify an initial value for a member variable (that is, either specifying an initial value when defining a member variable, or assigning a value to the property in the initialization block or constructor).

the final modified member variable must be assigned an initial value or an error is caused:

If you do not specify a default value when you define a member variable, and you do not specify an initial value for the member variable in the initialization block or constructor, the value of those member variables will always be: 0, ' \u000 ', false, NULL, loss of the meaning of existence.

Unlike ordinary variables, final member variables, including instance properties and class attributes, must be initialized by the programmer, that is, the system does not implicitly initialize the final member variable.

7. Use final modifier local variables:

since local variables are not initialized by the system, local variables must be manually assigned by the programmer, so the final decorated local variables can either specify a default value when defined, or do not specify a default value. If you do not specify a default value when you define it, you can assign an initial value to the final variable in subsequent code, but only once, and you cannot repeat the assignment. You can specify a default value when you define it, then you cannot assign a value to that variable in the following code.

the case of the final modifier parameter: Because the parameter is initialized by the system according to the parameters passed in when the method is invoked, the formal parameter with final decoration cannot be assigned

8. The difference between the final modifier base type and the reference type variable:

when you modify a base type variable with final, you cannot assign a value to the base type variable, that is, the value of the base type variable cannot be changed

The reference type variable holds a reference, and final only guarantees that the reference (address) will not change, that is, always referencing the same object, but this object can change.

The final-decorated reference type variable cannot be assignable, but the content of the object referenced by the reference type variable can be changed.

if the final-modified variable is of the base data type, and the value of the change is determined at compile time, the variable can be changed to constant processing. A constant name is made up of a number of meaningful words, the words are separated from the words, the final modified variable is a reference data type, the final variable cannot get the initial value at compile time, and must run to get the values.

9. Final Method

1. The final modified method cannot be overridden, and if for some reason you do not want a subclass to override a method of the parent class, you can use final to modify the method.

2. The final modification of the method can only be overridden, not cannot be overloaded

3. The final decorated class cannot have subclasses. For example, the Java.lang.Math class is a final class, which can not have subclasses. (The subclass inherits the parent class's internal data and can change the implementation details of the parent class method by overriding the parent class method, causing some unsafe factors; therefore, you can use final adornments when you need to ensure that a class cannot be inherited)

Java provides a final method in the object class: GetClass (). Because Java does not want any class to override this method.

10. Abstract method declaration format:
[modifier] Abstract Returns the value type method name ([form parameter table]);

the characteristics of abstract methods

U Abstract method has the keyword abstract before the return value type;

There is no method body for abstract methods, only method abstract signature, no implementation part of method.

the definition of an abstract method is a single line of statements, ending with a semicolon;

you are wrong to use the static modifier in an abstract method declaration.

11. Abstract class (a class that does not contain enough information to depict a specific object)

if an abstract method is defined in a class, the class must be defined as an abstract class.

[public] abstract class class name {

//class body (attributes, non-abstract, abstract methods, construction methods)

//Class body (initialization block, inner class, enum Class)


Μ an abstract class cannot create its own object, creating an abstract class object using new creates an error. An abstract class cannot be instantiated or an instance can be created.

when you inherit an abstract class from a Μ subclass, you should overwrite all abstract methods in the abstract class, or the subclass must also be defined as an abstract class.

Μ Abstract classes can contain six elements, such as properties, methods (common methods, abstract methods), construction methods, initialization blocks, inner classes, enumeration classes, and so on.

Μ classes that contain abstract methods, including directly defining an abstract class method, inheriting an abstract parent class but not fully implementing the parent class, and implementing an interface, but not fully implementing the abstract methods contained in the interface, can only be defined as abstract classes.

Μ Note

classes that contain abstract methods, including those that directly define an abstract method, inherit an abstract parent class, but do not fully implement the abstract methods that the parent class contains, can only be defined as abstract classes. Abstract classes, however, must contain abstract methods.

Methods of abstract methods and empty method bodies are not the same concept
public abstract void test ();
public void Test () {}

when a subclass implements an abstract method inherited from a parent class, its return value type, method name, and argument list must be the same as the parent class, but different subclasses have method bodies, and different subclasses can have different method bodies.


12. Note:

U final and abstract can never be used at the same time. (When the abstract is decorated, the class can only be inherited by the quilt class; it can only be overridden when the method is decorated, and the final decorated class cannot be inherited, the decorated method cannot be overridden)

U-Abstract cannot be used to decorate attributes, not to modify local variables, that is, there are no abstract variables, no abstract attributes, and abstract cannot be used to modify the construction method, and there is no abstract construction method. The constructor method defined in an abstract class can only be a common construction method.

U Static and abstract cannot modify a method at the same time, that is, there is no class abstract method. (static modifier, which indicates that this method belongs to the current class, that the method can be invoked through a class, and that if it is defined as an abstract method, an error occurs when the method is called by a modified class, and a method that does not have a method body is definitely wrong) .

The method of the abstract keyword modification must be overridden by its subclass to make sense, otherwise the method will never have a method body, so the abstract method cannot be defined as private access

13. The role of abstract classes

• code Reuse--subclasses can reuse attributes and Non-abstract methods in abstract parent classes;

You plan--abstract classes define the functions that all subclasses must implement by defining abstract methods, or they specify the interaction interface between their subclass objects and the outside world, because the method header of the abstract method already specifies the format of the method in the future of the quilt call.

14. Template pattern: Abstract class as a common template for multiple subclasses, subclasses extend and transform on the basis of abstract classes, but subclasses generally retain the behavior of abstract classes.

the simple rules for using templates are as follows:

1. The abstract parent class can only define some of the methods that need to be used, and the rest is left to subclasses to implement.

2. The parent class can contain specific methods that have already been implemented, usually by simply defining a generic algorithm whose implementation does not need to be implemented entirely by itself, but by virtue of subclasses.

U abstract classes cannot be instantiated, but abstract classes can be the type of a variable and the method parameter type, and you can assign an object of an abstract class subclass to the variable or to a method's argument. For example
Shape s=new Rectangle ();
public static String Showshapinfo (Shape item) {
if (item instanceof Rectangle) {
Rectangler= (Rectangle) item; Other code


Related Article

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: 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.