1. Class variable (static variable) and class method (static method) static
- Class variables are shared by all class objects. Class variables can be defined by class name Class variables and object names Class variable, but class name is usually used Class variable
Class variables are created when the class is loaded, so when we do not create an object instance, we can also access class variables through the class name - Static methods are similar to static variables. When developing your own tool classes, you usually use static methods for convenience. this and super cannot be used in static methods. Only static variables and static methods can be accessed.
package com.static_; public class TestStatic { public static void main(String[] args) { Student curry = new Student("Curry"); curry.count++; Student tom = new Student("Tom"); tom.count++; Student car = new Student("Car"); car.count++; System.out.println(curry.count); //3 System.out.println(tom.count); //3 System.out.println(car.count); //3 System.out.println(Student.count); //3 } } class Student{ private String name; public static int count; public Student(String name) { this.name = name; } }
2. Code block
2.1 common code block
- Code blocks are initialization blocks and members of classes. Similar to methods, they encapsulate logical statements in the method body.
- Put the same statement in a code block. No matter which constructor is called to create an object, the content of the code block will be called first
- The order of code block calls takes precedence over constructors
- When a subclass object is created, the constructor of the parent class is also called, so the code block in the parent class will also be called.
package com.codeblock_; public class CodeBlock01 { public static void main(String[] args) { Movie movie = new Movie("Spider-Man"); //result: // The movie screen opens // Advertising starts // The movie screen opens // Play spider man Movie movie1 = new Movie("Tang Tan 3", 20, "Chen"); //result // The movie screen opens // Advertising starts // The movie screen opens // Play Tang Tan 3 } } //Put the same statement in a code block. No matter which constructor is called to create an object, the content of the code block will be called first //The order of code block calls takes precedence over constructors class Movie { { System.out.println("The movie screen opens"); System.out.println("Advertising starts"); System.out.println("The movie screen opens"); } private String name; private double price; private String director; public Movie(String name) { this.name = name; System.out.println("play"+ name); } public Movie(String name, double price) { this.name = name; this.price = price; System.out.println("play"+ name); } public Movie(String name, double price, String director) { this.name = name; this.price = price; this.director = director; System.out.println("play"+ name); } }
2.2 static code block
- Static code block is also called static code block. Its function is to initialize the class and execute it as the class is loaded, and it will be executed only once. If it is an ordinary code block, it will be executed every time an object is created.
- Call the static member of the class, and the static code block will be called.
- Call the static member of the subclass, and the static code block of the parent class will also be called.
- Using static members of a class, ordinary code blocks are not called.
package com.codeblock_; public class CodeBlock02 { public static void main(String[] args) { AA aa = new AA(); /*Output: BB Static code block 1 of is executed AA Static code block 1 of is executed */ System.out.println(Cat.n1); /*Output: Animal Static code block 1 of is executed Cat Static code block 1 of is executed 30 */ } } class Animal { static { System.out.println("Animal Static code block 1 of is executed"); } } class Cat extends Animal{ public static int n1 = 30; static { System.out.println("Cat Static code block 1 of is executed"); } } class BB { static { System.out.println("BB Static code block 1 of is executed"); } } class AA extends BB{ static { System.out.println("AA Static code block 1 of is executed"); } }
-When creating an object, the order of calls is
1. Call static code block and static attribute initialization. (same priority, called in defined order).
2. Call ordinary code block and ordinary attribute initialization. (the priority is the same, and it is called in the defined order.)
3. Constructor.
package com.codeblock_; public class CodeBlock03 { public static void main(String[] args) { A a = new A(); // result: // getN1 called // The static code block of A is called // 22839 25421 26188 30847 9070 15577 12894 // getN2 called } } class A{ { System.out.println("A The normal code block of is called"); } private int n2 = getN2(); private static int n1 = getN1(); static { System.out.println("A The static code block of is called"); } public static int getN1() { System.out.println("getN1 Called"); return 100; } public int getN2() { System.out.println("getN2 Called"); return 100; } }
2.3 summary of call sequence when creating an object
Parent class static code and static attribute initialization - > subclass static code and static attribute initialization - > parent class common code block and common attribute initialization - > parent class construction method - > subclass common code block and common attribute initialization - > subclass construction method.
3.final keyword
- A class modified by final cannot be inherited by other classes
- Methods modified by final cannot be overridden by subclasses and can be inherited
- Attributes modified by final cannot be modified
- The attribute of final modification can be assigned in definition, code block and constructor.
- The final attribute modified by static can only be assigned in the definition and static code block
- The combination of final and static will not cause class loading
package com.codeblock_; public class CodeBlock04 { public static void main(String[] args) { System.out.println(Demo.i); //16 } } class Demo { public static final int i = 16; static { System.out.println("demo Be loaded"); } }
4.abstract
- When the method of a class is not implemented but only defined, modify the method with abstract, which is an abstract method. At the same time, modify the class with abstract, which is an abstract class.
abstract class Animal{ String name; int age; abstract public void cry(); }
- Abstract classes cannot be instantiated
- If a class inherits an abstract class, it must implement all abstract methods of the abstract class, otherwise it must also be defined as an abstract class.
5. Interface
structure
interface InterfaceName{ //attribute //Method (abstract method, default implementation method, static method) void function(); default void function1(){}; static void function2() {}; }
- Ordinary classes using interfaces must implement all methods in the interface
- Interface cannot be instantiated
- Abstract classes can implement interfaces without implementing all the methods in the interface
- A class can implement multiple interfaces
- The method in the interface defaults to public abstract
- The default attribute in the interface is public static final
- There is an inheritance relationship between interfaces
6. Internal class
- Internal classes can directly access private properties of external classes
- classification
Defined in a local location of a class (such as within a method)
1 local internal class (with class name)
2 anonymous inner class (no class name)
Defined on the member location of the class
1 member inner class (not decorated with static)
Static modifier (use static class 2)
6.1 local internal class
- You can directly access all members of the external class, including private
- The access modifier cannot be added because its status is a local variable, but final can be used
- Scope: only in the method or code block that defines it
- Accessing members in external classes: direct access
- The external class can create an internal class object in the method, and then call the inner class member.
- Other external classes cannot access internal classes
- If the members of the external class and the local internal class have the same name, follow the principle of proximity. If you want to access the members of the external class, use the external class name this. member
package com.innerclass; public class LocalInnerClass { public static void main(String[] args) { Outer r = new Outer(); r.m1(); } } class Outer{ private int n1 = 100; private void m2() { System.out.println("m2 Called"); } public void m1() { //The access modifier cannot be added because its status is a local variable, but final can be used //Scope: only in the method or code block that defines it //The external class can create an internal class object in the method, and then call the inner class member. final class Inner{ private int n1 = 800; public void f1(){ System.out.println("Inner of n1 =" + n1); System.out.println("Outer of n1 =" + Outer.this.n1);//You can directly access all members of the external class, including private m2(); //Accessing members in external classes: direct access } } new Inner().f1(); } }
6.2 anonymous inner class
- It is equivalent to inheriting a class or implementing an interface
package com.innerclass; public class AnonymousInnerClass { public static void main(String[] args) { Outer01 outer01 = new Outer01(); outer01.method(); } } class Outer01 { private int n1 = 10; public void method() { //Anonymous inner class based on interface //Class is used only once and will not be used again //Compilation type A of tiger //The running type of tiger is an anonymous inner class A tiger = new A() { @Override public void cry() { System.out.println("The tiger calls...."); } }; Father father = new Father("jack"){ @Override public void test() { System.out.println("Anonymous inner class test"); } }; tiger.cry(); father.test(); } } interface A { public void cry(); } class Father { public Father (String name){ } public void test() {} }
6.3 member internal class
- You can access all members of an external class
- Access modifiers can be added
package com.innerclass; public class MemberInnerClass { public static void main(String[] args) { Outer02 outer02 = new Outer02(); outer02.t1(); Outer02.Inner02 inner02 = outer02.new Inner02(); //Create internal class object inner02.say(); } } class Outer02 { private int n1 = 10; public String name = "Zhang San"; class Inner02 { public void say() { System.out.println("n1 = " + n1 + " name = " + name); } } public void t1() { Inner02 inner02 = new Inner02(); inner02.say(); } }
6.4 static internal class
- You can only access static members of external classes, not non static members.
- Access modifiers can be added
- It can be accessed directly through the external class name
package com.innerclass; public class StaticInnerClass { public static void main(String[] args) { //Two ways to create inner classes Outer03.Inner03 inner03 = new Outer03.Inner03(); inner03.say(); Outer03.Inner03 inner031 = Outer03.Inner03.getInstance(); inner031.say(); } } class Outer03 { private int n1 = 10; private static String name = "jack"; static class Inner03 { //You can only access static members of external classes, not non static members. //Access modifiers can be added public void say() { System.out.println(name); } public static Inner03 getInstance() { return new Inner03(); } } }