static
Static can be used to modify member methods of a class or member variables of a class.
A static variable
Static variables are also called static variables. The difference between static and non-static variables is that static variables are shared by all objects and have only one copy in memory, which is initialized if and only if the class is first loaded. Non-static variables are owned by the object, initialized when the object is created, and have multiple copies that do not affect each other.
In the following example, age is a non-static variable, p1 prints the result: Name:zhangsan, age :10; Name:zhangsan, age :12 if age is static, p1 prints Name:zhangsan, age :12, because static variable has only one copy in memory.
public class Person {
String name;
int age;
public String toString(a) {
return "Name:" + name + ", Age:" + age;
}
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "zhangsan";
p1.age = 10;
Person p2 = new Person();
p2.name = "lisi";
p2.age = 12;
System.out.println(p1);
System.out.println(p2);
}
/**Output * Name:zhangsan, Age:10 * Name:lisi, Age:12 *// / ~
}
Copy the code
A static method
Static methods are generally called static methods. Static methods can be accessed independently of any object, and can be invoked by class name.
public class Utils {
public static void print(String s) {
System.out.println("hello world: " + s);
}
public static void main(String[] args) {
Utils.print("Programmer Big Bin"); }}Copy the code
Static code block
Static code blocks are executed only once when the class is loaded. In the following example, startDate and endDate are assigned at class load time.
class Person {
private Date birthDate;
private static Date startDate, endDate;
static{
startDate = Date.valueOf("2008");
endDate = Date.valueOf("2021");
}
public Person(Date birthDate) {
this.birthDate = birthDate; }}Copy the code
Static inner class
In static methods, the use of a non-static inner class depends on an instance of the outer class, which means that an instance of the outer class needs to be created before it can be used to create a non-static inner class. Static inner classes do not.
public class OuterClass {
class InnerClass {}static class StaticInnerClass {}public static void main(String[] args) {
OuterClass. This cannot be used directly to create an instance of InnerClass in static methods
// You need to create an instance o of the OuterClass and use O to create an instance of the InnerClass
// InnerClass innerClass = new InnerClass();
OuterClass outerClass = new OuterClass();
InnerClass innerClass = outerClass.new InnerClass(a);
StaticInnerClass staticInnerClass = new StaticInnerClass();
outerClass.test();
}
public void nonStaticMethod(a) {
InnerClass innerClass = new InnerClass();
System.out.println("nonStaticMethod..."); }}Copy the code
final
-
Basic data types cannot be modified if they are final and are constants. If an object reference is final, the reference can only point to that object, not any other object, but the object itself can be modified.
-
Final methods cannot be overridden by subclasses
-
Final modified classes cannot be inherited.
I am programmer Dabin, public name, welcome everyone to pay attention to and like oh ~