1.static keyword "30 minutes to start teaching in 22-speaking video"
2.static decorated properties: No matter how many objects a class generates, all of these objects work together to use a unique static member variable, and one object modifies that static member variable, and the value of that static member variable for the other object changes as well. If a member variable is static, then we can use it in the same way as the class name. Member variable (Java recommends this way).
Package com.cl.abstracttest; Public class statictest { publicstaticvoid main (string[] args) { new mystatic (); New mystatic (); = ten; System.out.println (mystatic2.a); }} class mystatic{ int A;}
The result is: 0
change int A to static int a the result becomes 10.
package Com.cl.abstracttest; public class Statictest { static void main (string[] args) {MyS Tatic mystatic = mystatic (); MYSTATIC.A = 10; // Use the class name directly. Member Variable mode System.out.println (MYSTATIC.A); }} class mystatic{ static int A;}
3.static Modification Method: The static method is called a method. For static methods, you can use the class name. Method name to access
Packagecom.cl.staticandfinal; Public classStaticTest2 { Public Static voidMain (string[] args) {/*method One MyStatic2 s = new MyStatic2 (); S.output ();*/ //Method Twomystatic2.output ();//need to change method to static}}classmystatic2{ Public Static voidoutput () {System.out.println ("Output"); }}
4. static methods can only inherit and cannot be overridden (override). subclasses cannot override parent class methods and can overload parent class methods
Packagecom.cl.staticandfinal; Public classStaticTest3 { Public Static voidMain (string[] args) {m M=NewN (); M.output (); } }classm{ Public Static voidoutput () {System.out.println (M); }}classNextendsm{ Public Static voidoutput () {System.out.println (N); }}
The output is: M
If you put
M m = new n (); instead n m = new n (); The result is n
5.final Keyword: Final can modify properties, methods, classes.
6.final Modified class: When a class is modified by final, it means that the class is a final state class and cannot be inherited.
Java SE 23rd talk----the static keyword