Keywords final, static

Posted by unknown on Thu, 16 Dec 2021 14:28:49 +0100

Keyword final

The meaning is final and invariable

final attribute and temporary variable cannot be modified once assigned

Attribute assignment can be declared and directly assigned private final string name = "zhangsan";, You can also assign values in constructors or non static code blocks

class Al {
private final String name = "zhangsan" ;\
}
class B1 {
private final String name;
public B1(){
name ="No parameters";
	}
public B1(int kk) {
name ="integer argument ";
	}
}
class C1 {
private final String name;
    {
name ="Non static code block";
	}
}

syntax error

class B1 {
private final String name;
{ //Non static code blocks are executed before the constructor executes. The fina1 attribute is initialized in a non static code block, but modified in the constructor
name="Non static code block";
}
public B1() {
name ="No parameters";
}
public B1(int kk){
name ="integer argument ";
	}
}

final method. It is not allowed to override definitions in subclasses

class Parent{
public final void pp()[}
class son extends Parent{
public void pp(){}//Syntax error. Because the pp method in the parent class has been declared final, modification is not allowed
}

The final class indicates that this class is not allowed to be inherited

final class Parent{}
class son extends parent{}//Syntax error, because the parent class has been declared as fina1 type, it is not allowed to define a child class

It is not allowed to be inherited. It can also be implemented by using private constructors [all constructors must be private]

//Cannot inherit
public class Parent{
private Parent(){}//Because it is used for custom constructors, the system no longer provides constructors. There is only one private constructor in this class
public class Son extends Parent{}//Syntax error

Allow inheritance

public class Parent{
private Parent(){}
public Parent(int age){}
}
public class Son extends Parent{
public Son(){
super(100);//Call the constructor Parent(int) in the Parent class
}

Note: string StringBuilder and StringBuffer are final classes, so subclasses are not allowed to be defined

summary
The final attribute can be declared and assigned directly or in the constructor
The final temporary variable can be assigned at the same time as the declaration or before the ━ th use
Note: once a variable of final type is assigned, it is not allowed to modify, but if it is a complex type, it is not allowed to modify the address. but
Yes, attributes can be modified

The final method indicates that this method does not allow redefinition in subclasses (override \ override)

The final class indicates that this class is not allowed to be inherited

static keyword

Mainly used to decorate members
It is generated with the loading of the class, destroyed with the disappearance of the class. It takes precedence over the object and is accessed directly with the class name

Static properties

There is only one attribute of all current classes. All current class objects share this attribute. Syntax:

public class A{

Scope qualifier[private/package/protected/public] static Type variable name;//Simultaneous assignment of declarations is allowed
}
//The calling method can be called as the attribute of the object [new A(). Variable], or through [class name. Variable]

Typical application

public class Ai
	private static int count=0;
	public A(){
		count++;
	}
public int getcount({ return count;}
}

Static method

Because you can use the class name directly The static method is called directly in the form of "method name". The object may not be built when the static method is executed, so the keyword used to specify the object such as this/super is not allowed in the static method

Of course, it is allowed to create objects and call object methods in static methods

Static methods can only directly access static members, not non static members

grammar

public class class{
Scope qualifier static Return value type method name(parameter list){}
}
    //You can call with [new class (). Method name (argument)] or [class. Method name (argument)]

It is not allowed to use super or this to refer to objects in static methods. Of course, it is allowed to create objects and call them in static methods

Static methods can only directly access static members, not non static members, unless they create their own objects

public class A{
private int age;
private static int count=O ;
public void dd(){}//Non static method
public static void ff(){}//Static method
public static void pp(){
this.name ;//Syntax error, because this and super are not allowed in static methods
dd();//Syntax error, because only static members can be accessed directly in static methods
System.out.println(age);//Syntax error, because only static members can be accessed directly in static methods
System.out. println(count);//correct
ff();//correct
A.ff();//correct
system.out.print1n(new A().age); //Correct grammar
new A().dd();//Correct grammar
new A().count;//correct
new A().ff();//correct
      }
}

Static code block

It is actually an anonymous method, but it will be executed automatically after the class is loaded. Syntax can only be executed once because of anonymity

public class class{
static {//Static code blocks can be defined multiple times, and all static code blocks will be automatically executed after class loading
    processing logic ;
	}
}

Non static code block

It's actually an anonymous method, but it's automatically executed once before building the object. Syntax can only be executed once because of anonymity

public class class{
 {//Static code blocks can be defined multiple times, and all static code blocks will be automatically executed after class loading
    processing logic ;
	}
}

Special execution timing

When the class is loaded, it will automatically give priority to static static attributes and static static code blocks. These two priorities are the same, so whoever defines them before will implement them first

Non static attributes and non static code blocks are processed only when the new object is executed. These two priorities are the same, so whoever defines them first will implement them first

The constructor is executed last. When executing constructors, the father is first followed by the child

class A2 {
static {
System.out.println("A2 Static code block");
public A2(){
System.out.println("A2 Parameterless constructor");
System.out.println("A2 Non static code block");
class B2 extends A2{
static {
System.out.println("B2 Static code block"");
System.out.println("B2 Non static code block");
public B2() {
System.out.println("B2 Parameterless constructor");

results of enforcement
A2 static code block

B2 static code block

A2 non static code block

A2 parameterless constructor

B2 non static code block

B2 parameterless constructor

Using static

·When the data of all member variables are the same, you can use static to modify the static property, so that multiple objects can share a static property
·Method is non static if it accesses unique data (non static members). If you need a method that does not hold data, the method is static. For example, math abs(-123)
·If all the methods in the class are static, there is no point in building the object of the class, so the constructor will generally be set to private. For example, tool classes
Variable number of parameters
When the parameters of a method are uncertain, you can define a variable number of parameter methods. Syntax: variable type... Variable name

public class Af
public void pp(int. . . args){//args can be used directly as an integer array
for(int i=0;i < args.length; i++)
System.out.println(args[i]);
public static void main(string[] args){
new A().pp();//Can be called, but not through pp(nu11)
new A().pp(1);//Can call
new A().pp(1,2,3,4,5)//Can call
      }
}

Topics: Java Eclipse