Common keyword resolution in Java

  • 2020-04-01 03:27:08
  • OfStack

In this paper, the common Java keywords final, static, super, this are analyzed in detail to facilitate readers' reference. The details are as follows:

A final,

1) modify class(class) :

Indicates that the class is not inheritable

2) modification of method

Indicates that the method method cannot be overridden (@override)

3) modify the variable

Indicates that the value of the variable cannot be changed later, and is often used to modify immutable constants

Final, final, final, final, final, final, final, final, final, final, final, final, final

Static (commonly used in the second and third methods)

1) modify class(class) :

(not often used) can only be used to modify inner classes, ordinary classes are not allowed to declare this way

2) modification of method

A static method is one that belongs to a class, not to an object. Instead of creating an object, you can call this method using the class name.
(the bytecode of the class is loaded into memory when the program is executed, the class variable is allocated memory when the class does not create an object, and the instance variable is allocated memory space when the concrete object is created.)

3) modify the variable

Similar to 2), the variable is Shared at the class level, and no object needs to be created, but the change of the value of the variable is holistic. That is to say, if object A changes the value of A static variable by operating on it, then object B operates on the changed static variable.

Note: static code blocks are called static code blocks. Static code blocks are executed first when the JVM loads a class. No matter where they are placed, they are executed first before the normal code blocks are executed. There can be multiple static blocks of code that are executed by the JVM in turn in the order of their occurrence, with each block being executed only once.

The this and super keywords cannot appear in static methods.

Iii. Super :(referring to the reference to the parent class)

1) related to Class (parent Class, also known as super Class) :

Through super(parameter 1, parameter 2... , parameter n), and call the superclass constructor;

2) related to Method

Super. Methord(parameter 1, parameter 2... , parameter n) call the method whose superclass is overridden;

3) related to variable

Super.variable, which calls a variable of the parent class

Note: super must be used on the first line of the subclass constructor;

When the parent class does not have a constructor with no arguments, the subclass needs to explicitly call the parent class's constructor using super, which is a reference to the parent class

This :(reference to the current object)

1) Class related:

This (parameter 1, parameter 2... , parameter n), this is the ordinary constructor, only the constructor (others) can call the constructor (this), ordinary function can not call the constructor.

2) Method:

Invokes the method of the current object

3) Variable

Invokes the variable of the current object

Note: this (); And super (); The explicit call constructor can only be placed on the first line of the construct;

The enclosing method.. (a); Super. Method.. (a); Can be put anywhere as a common way to make;

There is a big difference between a constructor and a method using the keyword this. Method reference this points to an instance of the class that is executing the method. Static methods cannot use the this keyword because static methods do not belong to an instance of a class, so this has nothing to point to. The constructor's this points to another constructor in the same class with a different argument list.


Related articles: