The constructor is also called a constructor.
Each class has at least one constructor.
Constructor is a special method with the following features.
(The Method Name of the constructor must be the same as the class name.
(The constructor does not have a return type and cannot be defined.
(The main function of constructor is to complete object initialization.
(Constructor. If no constructor is defined when a class is defined, the compilation system automatically inserts a default constructor without parameters. This constructor does not execute any code.
(The constructor can be overloaded and distinguished by the number, type, or order of parameters.
(6) subclass cannot inherit the constructor.
If the constructor is not explicitly written to a class, an implicit constructor will exist in this class by default.
Therefore, we recommend that you write a non-parameter constructor when defining a class.
When instantiating an object, it does not indicate which constructor to call. By default, a constructor without parameters is called.
Definition constructor:
System. out. println ("I Am a lowercase letter:" +}
Call the constructor:
Person p1 = Person(); Person p2 = Person("a"); }
Static
Load as the class is loaded
Takes precedence over the existence of objects.
Shared by all objects
Can be called directly by Class Name
It can be used to modify methods and member variables.
Usage notes
Static methods can only access static members, but non-static methods can access static members.
Static methods cannot be used
Not used
Use
Static
No
When is it suitable for use?
Generally used when writing tool Methods
Example:
Person () System. out. println ("construction method without Parameters "}
String name; String sex; String address; name = "Xiao Li"; address = "Heaven and Earth"; System. out. println ("dinner" name = "Xiao Li"; address = "Heaven and Earth"; System. out. println ("Walking "}
Call:
Person p = p. name = "Xiao Ming"; Person. name = "Xiao Li"; System. out. println (}
Final
Java
Final
Final
Final
Final
Note: You can use final when a class does not need a subclass.
Final class:
B A }
Final method:
Test () System. out. println ("I'm the final method" B test () System. out. println ("I'm the final method "}
Final variable:
number=20 age; A( .age= number++; }
Final constructor:
A () System. out. println ("constructor "}
Final reference data type:
StringBuilder sb = StringBuilder("123" sb.append("456");
Although the final modified variable is called a constant, the value cannot be modified. However, the value of the referenced variable can be modified, but the address cannot be modified.