This
- be able to use this represents a property in a class------this.name=name
- The ability to use this to emphasize the invocation of a method of this class
- The ability to use this to invoke the constructor of this class ------This (); Call the no-participation method in this class
- be able to use this represents the current object (the object that invokes the method)----The most important use
Static:
- Declaring a property---------property is a global variable
- Declaring methods---------Person.setname ("Tom"); Calling the static method with the class name
Final
- A class that uses final declaration cannot have child classes
- Methods that use Final declaration cannot overwrite a quilt class
- A variable that uses final declaration becomes a constant and cannot be changed (all uppercase letters are required when using final declaration variables)
Spuer:
- Calling the parent class's constructor method
- Calling the normal method of the parent class
- Calling the properties of the parent class
Note: This and super must be placed in the first line within the constructor method, formally for this reason, this () and super () cannot exist at the same time within a constructor method
Common block of code: a block of code defined directly in a method or statement
public class Codedemo01{public static void Main (String args[]) {{//plain code block int x = 30;//is a local variable System.out.println ("Normal code block -X = "+ X";} int x = 100;//the same System.out.println as the local variable name ("Outside the code block--X =" + x);}};
Building Blocks:code blocks that are written directly in the class
Class Demo{{//writes a block of code directly in the class, called the building Block System.out.println ("1, building block. ") ;} Public Demo () {//define construction Method System.out.println ("2, constructor method. ") ;}}; public class Codedemo02{public static void Main (String args[]) {new demo ();//Instantiate Object new Demo ();//Instantiate Object new Demo ();//Instantiate to Like}};
Execution Result:
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
Static code block: using Statickeyword declaration the code block
Class Demo{{//writes a block of code directly in the class, called the building Block System.out.println ("1, building block. ") ;} static{//uses static, called Static code block System.out.println ("0, Static code block");} Public Demo () {//define construction Method System.out.println ("2, constructor method. ") ;}}; Public class codedemo03{static{//defines the static block System.out.println ("code block defined in the class where the Main method resides") in the class where the main method resides;} public static void Main (String args[]) {new demo ();//Instantiate Object new Demo ();//Instantiate Object new Demo ();//Instantiate Object}};
Execution Result:
Blocks of code defined in the class where the Main method resides
0. Static code block
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
Conclusion:
- Static code blocks take precedence over the Main method run
- Static code blocks defined in a class take precedence over building blocks (no matter how many objects are generated, run only once)
This,static,super and Finalkeyword and code blocks in Java