D 6. this and static keywords in java

Introduction:

    D 5. java method overload How do two objects of the same type call a method? So let's take a look at this example

I. this

class cat{
	void print(int i) {
		System.out.println("There is "+i+" cat.");
	}
}
public class ThisObject {
	public static void main(String []args) {
	cat c1 = new cat();
	cat c2 = new cat();
	c1.print(1);
	c2.print(2);
	}
}

You may find that these two objects are called by parameters (that is, passing a reference to the object being manipulated to a method). If you want to get a reference to the current object inside the method, you must know the this keyword. This keyword can only be used inside a method to represent a reference to "the object that called the method.". If you call a method in the same class in a method, you do not need to use this keyword, and this reference will be automatically applied to the method in the same class. Therefore, this keyword is used only when the reference of the current object needs to be clearly indicated. For example:

public class ThisObject {
	int i = 0;
	ThisObject cat() {
		i++;
		return this;
	}
	void print() {
		System.out.println("i= "+i);
	}
	public static void main(String []args) {
		ThisObject t = new ThisObject();
		t.cat().cat().cat().print();
	}
}

Output: i= 3

 

Here we need to explicitly return the reference of the current object (so that we can perform multiple operations on the same object in a statement), so we use this keyword after return. This keyword can also be used to pass the current object to other methods. For example:

class cat{
	public void eat(Apple apple) {
		Apple peeled = apple.getPeeled();
		System.out.println("jack");
	}
}
class Peeler{
	static Apple peel(Apple apple) {
		return apple;
	}
}
class Apple{
	Apple getPeeled() {return Peeler.peel(this);
	}
}
public class ThisO {
	public static void main(String []args) {
		new cat().eat(new Apple());
	}
}

Output: Jack

 

We create the cat object in the main method, and call the eat method in the main method. The parameter is the apple object. In the main method, we call the getPeeled() method in apple. We need to return the current object reference, then return the apple object, and finally output jack. In this case, Apple needs to call the external Peeler.peel method and pass itself to the external method, so we use this keyword. Of course, we can also use the this keyword to call constructors in the constructor (we can only call a constructor). For example:

public class ThisO{
	int Count = 0;
	String s = "cat";
	ThisO(int c){//Constructor of int parameter
		Count = c;
		System.out.println("Constructor int args Count = "+c);
	}
	ThisO(String ss){//Constructor of String parameter
		System.out.println("s = "+ ss);
		s = ss;
	}
	ThisO(String s,int c){//Constructor of two parameters
		this(c);
		//this(s); trying to call another constructor, error!
		this.s = s;//Clearly assign data idioms, different from parameter s
		System.out.println("String & int args");
	}
	ThisO(){//Parameterless constructor
		this("pig",47);
		System.out.println("default constructor (no args)");
	}
	void PrintCount() {
		//this(11); we can't call the constructor in a method other than the constructor, ha ha
		System.out.println("Count = "+Count+" String= "+s);
	}
	public static void main(String []args) {
		ThisO t = new ThisO();
		t.PrintCount();
	}
}

Output:

 

In the above example, we can see that only one call can be made to the constructor, but not two calls. We can also see that the compiler prohibits other method calls to the constructor except for the constructor. In the example, the syntax of this.s = s is used because the external parameters and data member names are consistent. this.s is used to represent data members. In fact, we often use this syntax, such as void cat (int i) {this. I = I;} to initialize.

 

 

Two, static

The opposite of this is static. Static methods are methods without this. Non static methods cannot be called inside static methods, but vice versa. One of the uses we mentioned before is that static is used if you call a method without creating an object. But we can't have a lot of static. Although there are no global variables in java, static can play this role. (in C language, we are all told whether we can use a large number of global variables, so it is the same in java.).

Here is a summary of object initialization and cleaning. For the garbage collection mechanism, you can check the resources. Initialization also has many things that we should pay attention to, such as the initialization of arrays (the initialization of arrays in java avoids overstepping), and the initialization of static types. And what we often encounter in c + +, we need to make clear the order of initialization (this type of problem is often mentioned in polymorphism, which is also worthy of our attention).

 

Conclusion:

You will find that I mentioned local and global variables in the static keyword, so they must have a certain range. Next, let's look at the access permission range. If there is any mistake, please point out. Thank you.

 

 

79 original articles published, 182 praised, 10000 visitors+
Private letter follow

Keywords: Java C

Added by jakeone on Tue, 11 Feb 2020 13:53:49 +0200