Commonly used keywords in java 1

One.static

static keyword

Static can be used to modify methods and constants, but static can also be used to modify inner classes. Ordinary classes are not allowed to be declared static, only inner classes can. An inner class modified by static can be used directly as a normal class without instantiating an outer class.

(1) Inner classes modified by static

public class option {
	public static class add{
		add(){
			System.out.println("I am an inner class");
		}
	}
public static void main(String[] args) {
	new option.add();
}
}
result:
I am an inner class

 Classes that are not modified by static can only be called in the following ways:

public class option {
	public  class add{
		add(){
			System.out.println("I am an inner class");
		}
	}
public static void main(String[] args) {
	option op=new option();
	op.new add();
}
result:
I am an inner class

(2) Methods and properties modified by static:

The method modified by static is stored in the public data area and executed only once in the entire life cycle;

The method modified by static does not belong to a specific object, so there is no need to create an object to call properties and methods; it is not necessary to create a corresponding object but can use the object to operate;

2. this and super

2.this keyword

this represents the class name and the current class object in java

(1) this(value);

this(value) represents the current class name, class name(value)==this(value)==construction method;

this (value) can only be written in the body of the constructor, and must be the first line of valid code;

Direct or indirect calls between constructors are not allowed in java, so the keyword this is used to indicate the current constructor;

(2) this. property or this. method

This in this.property or this.method represents the current class object;

this.property or this.method can only be written in non-static method body;

this.property or this.method is used to solve the problem of proximity when global variables and local variable parameters have the same name;

3. super keyword

super can represent the parent class name and refer to the parent class object in java

(1) super(value)

The super in super(value) represents the class name of the parent class; the class name of the parent class (value)==super(value)==the constructor of the parent class;

super(value) can only be written in the body of the subclass's constructor, and must be the first line of valid code

(2) super. property or super. method

In super.property or super.method, super represents a reference to the parent class object;

super can only be used in non-static method bodies to prevent objects from being referenced without creating them;

三、final、finally、finallize

1. final can be used to modify classes, variables and methods; classes modified by final cannot be inherited; variables modified by final are constants; methods modified by final are constants

2.finally is used in a try-catch block; the code in finally will be executed regardless of whether the try block or the catch block executes;

3. The finalize garbage collection system, the garbage collector will treat objects that override the finalize() method in particular. Normally, during garbage collection, an unreachable object is destroyed immediately. However, objects that override the finalize() method are moved to a queue, and a separate thread traverses the queue, calling each object's finalize() method. After the finalize() method is called, these objects become real garbage, waiting for the next round of garbage collection.

四、break、continue、return

1.break is used in a switch code block or loop body to end the switch code or loop body;

2. Contiune: It can be used in the loop body of java to end the execution of this loop;

3.return: used in the loop body or method body; directly end the current loop body or the entire method body;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326133135&siteId=291194637