Final and static perceptions

When the final modification is the basic data type and String, if you know its value during compilation. The compiler will treat it as a compiler constant, so it is equivalent to directly accessing the constant. Only in this case can it be handled like this.

final String s="hello";
String a="hello2";
String b="hello";
String c="2";
String d=b+2;
String e=s+2;
System.out.println(a==d);//false
System.out.println(a==e);//true
//Because it is not a basic type, it is not equivalent to directly change it (I don't understand it)
final String s="hello";
String a="hello2";
String b="hello";
String c="2";
String d=b+c;
String e=s+c;
System.out.println(a==d);//false
System.out.println(a==e);//false

 

The final modified variable must be assigned before it can be used (the final class)

The static modified variable is converted to an initial value, which can be defined in the body of the class (modify global variables), but cannot be defined in methods (modify local variables), and the address is the same in different instances and stored in the global area.

Local variables: "formal parameters" in Java methods are local variables

                  The variables in the method are "actual parameters"

Static is not allowed to modify external classes, because in this way they will all be loaded when the class is loaded. Normally, it should be called only when it is used

A static method can access only sttic data,It can not access non=static data(instance variables)

Instance method---construction method, class method---static method

Guess you like

Origin blog.csdn.net/weixin_40728070/article/details/90813405