Java keywords-this, super, static, final


One, this keyword

1.this overview

  This is a keyword in the Java language. The following figure shows the memory diagram of this.

Insert picture description here
  this can be seen as a variable, it is a reference, stored in the Java virtual machineHeap memoryInside the object , this reference saves the memory address of the current object pointing to itself. Any java object in the heap memory has a this, that is to say, creating 100 java objects corresponds to 100this respectively.

  This refers to the current object and can be used in instance methods and construction methods. The syntax format is respectivelythis.和this(…)

  This cannot appear in the method with static, because the method with static is loaded with the loading of the class, before the creation of the object. It can appear in the instance method and represents the current object. In most cases, this can be omitted, and it cannot be omitted only when the local variable and the member variable in the instance method have the same name.

2.this is used in the construction method

 There is another usage of this, which is used in the first line of the constructor (the rule can only appear in the first line),Call other constructors in this class through the current constructor, Its purpose is for code reuse.
Call format:

this (actual parameter list)

For example:

//无参构造
 public Date(){
    
    
            this.year=1997;
            this.month=12;
            this.day=24;
        }
 //有参构造  
 public Date(int year,int month,int day){
    
    
            this.year=year;
            this.month=month;
            this.day=day;
            
        }

 We can find that the code in the parameterless construction method is the same as the code in the parameterized construction method. The code is not reused. At this time, you can use this (the actual parameter list) in the parameterless construction to call the parameterized construction Method, let the code be reused.

   public Date(){
    
    
   //注意this()语法只能出现在构造方法的第一行!!
            this(1997,12,24);
        }
        public Date(int year,int month,int day){
    
    
            this.year=year;
            this.month=month;
            this.day=day;

        }

Two, super keyword

1.super overview

In fact, super is not a reference, but a keyword. Super represents the part of the current object inherited from the parent class .

  • Both super and this can be used in instance methods
  • Super cannot be used in static methods, because super represents the supertype characteristics of the current object. This does not exist in static methods, so super cannot be used.
  • The subclass redefines the instance variable or instance method of the same name of the parent class. If you want to access the instance variable of the parent class in the subclass, super cannot save it.

2.Super is used in the construction method

super (actual parameter list)

Indicates that the construction method of the parent class is called during the execution of the subclass construction method.
It and this (the actual parameter list) are only allowed to appear in the first line of the constructor , so these two lines of code cannot coexist.
If super() or super (argument list) is not displayed in the subclass construction method, then super() is defaulted.

3. The essential difference between this and super

This points to the current object, and super does not point to any object in the strict sense, but only represents the characteristics of the parent class in the current object.

Three, static keyword

1.static overview

 Static is a keyword in the Java language, which can modify variables, methods, and code blocks . Modified variables are called static variables, modified methods are called static methods, and modified code blocks are called static code blocks.

 In java languageAnything modified by static is related to the class. You don’t need to create an object. You can access it directly through the “class name” . Even if you use “reference” to access, it has nothing to do with the objects in the heap memory at runtime.

2. Static variables

 Variables in java are divided into: local variables and member variables. Member variables are divided into static variables (modified by static) and instance variables (not modified by static). Regarding the situation and contact of the three, you can refer to my other blog for detailed explanations. Local variables, static variables, instance variables

3. Static code block

  1. Syntax format:
{
    
    
		//静态代码块
		static{
    
    
				java语句;
		}
}
  1. scenes to be used

The static code block is executed when the class is loaded, and only once. The static code block is actually a special moment prepared by the Java language for programmers. This moment is the moment of class loading. If you want to execute a piece of code when the class is loaded, then you can use the static code block.

4. Static method

 First of all, we make it clear that the method is actually a behavior action. When we think that the action needs the participation of the object when it is triggered , this method should be defined as an instance method .

 When the action is triggered without the participation of the object , we can define it as a static method , which is more convenient to call. You do not need to create an object, you can access it directly by using the class name. In actual development, the methods in the "tool class" are generally defined as static methods.

Four.final keywords

usage:

(1) Modification class (including external class and internal class)

Indicates that this class cannot be inherited and has no subclasses

Improve safety and improve program readability.

(2) Modification method

Indicates that this method cannot be overridden

(3) Modified variables (member variables (class variables, instance variables), local variables)

It is called a constant, which means that the value of this variable cannot be modified

(4) Modify the reference data type to indicate that the address value cannot be modified.

Note: If a member variable is modified with final, it must be manually assigned, and once the value is assigned, it cannot be modified, that is, there is no set method.

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110223193