Basic knowledge of JAVA (6)

5.9 this keyword

Use this to emphasize the methods in this class. In addition to this, there are:
(1) Represents the attributes in the class.
(2) You can use this to call the construction method of this class.
(3) This represents the current object.

5.9.1 Use this to call properties in this class

5.9.2 Use this to call the constructor

If there are multiple construction methods in a class, you can use the this keyword to call each other.

class Person44{
    
    
	private String name;	//姓名
	private int  age;	//年龄
	public Person44() {
    
    	//无参构造
	  System.out.println("一个新的Person对象被实例化。");
	}
	public Person44(String name,int age) {
    
    
		this.name=name;		//为name属性赋值
		this.age=age;		//为age属性赋值
		this();//错误的调用,只能放在构造方法的首行
	}
	public String getInfo() {
    
    	//取得信息
		this();
		return "姓名:"+name+",年龄:"+age;
	}
}

An error occurred during compilation of the above program, because this() can only be placed in the first line of the constructor.
In addition, when calling the construction method for this(), you must leave a construction method as an exit, that is, there is at least one construction method in the program that does not use this to call other construction methods.

5.9.3 this represents the current object

Explanation about the current object.
Now suppose there are three people, Zhang San, Li Si, and Wang Wu talking. If the person speaking is Zhang San, then the "person who is currently speaking" is Zhang San; and so on, so as long as it is the "person who is currently speaking" ", then it means the current object.

5.10 static keyword

From the concepts explained above, if you use a class, you need to open up stack memory and heap memory separately, and store the properties of the object in the heap memory. Each object has its own properties. If there are some properties, all objects Shared, it must be declared as a static attribute. If a method in a class is not directly called by the object but by the class name, it can be declared as a static method.

5.10.1 Use static to declare attributes

If you use static to declare properties in your program, this property is called a global property (some are also called static properties).

A memory area commonly used in Java.
There are mainly 4 memory spaces in Java. The names and functions of these memory spaces are as follows:
(1) Stack memory space: saves all object names (more precisely, saves the address of the referenced heap memory space).
(2) Heap memory space: save the specific attribute content of each object.
(3) Global data area: save static type attributes.
(4) Global code area: save all method definitions.

It is most appropriate that the public properties of a class should be modified by the class, because the user does not know how many objects a class has. The attributes declared using static are called class attributes.

Class attribute call
class name.static attribute

5.10.2 Use static declaration method

Static can be used when declaring properties or methods. Declaring methods with it is sometimes called "class method" and can be called directly by the class name.

Explanation about the definition of static method.
In the actual development, if you want to define a method in a class. Generally, non-static methods (ordinary methods) are the main ones, and static methods are generally defined in one case: this method does not want to be called by instantiating objects, then there are two factors at this time:
(1) This class does not Provided with common attributes, it is meaningless to generate instantiated objects in this way;
(2) This class cannot directly instantiate objects, and can only use static operations.

It should be noted. Non-static declared methods can call static declared properties or methods. However, the method declared by static cannot call the properties or methods declared by non-static types.

Static cannot call any non-static content, because all properties and methods in the program can only be called after the object has opened up the heap memory, and static methods can be called by the class name when the object is not instantiated.

5.10.3 related applications of static

The static attribute is shared by all objects, so you can use the static attribute to count how many instantiated objects a class has produced.

You can use static to perform automatic naming operations for objects.

5.10.4 understand the main method

public: Indicates that this method can be called externally.
static: Indicates that this method can be called directly by the class name.
void: The main method is the starting point of the program, so no return value is required.
main: The system specifies the name of the method called by default, and finds the name of the main method by default during execution.
String args[]: Represents the parameters at runtime. The parameter passing format is "Java class name parameter 1 parameter 2 parameter 3···".

Use the supplement of static definition method.
If a method is to be directly called by the main method, it must be declared in the following format: "The return value type method name (parameter list) of the public static method {}".

Guess you like

Origin blog.csdn.net/qq_43480434/article/details/112795406