The class has three types of members: field, method, and nested class and nested interface. The learning class can be expanded from the Declaration, modifier, initialization (including Class Object creation) and usage of the three members. Here we mainly talk about fields and methods.
Statement
Field Declaration: modifier (optional), type name, field name, initialization clause (optional );
The method declaration is modifier, return type, method signature (method name, parameter list), exception, and method body.
Modifier
The table is summarized as follows:
Each modifier can be expanded into a very large topic. In the table, "yes" indicates yes, and "no" indicates no.
Initialization
The most direct way to initialize a field is to use the initialization clause. Field initialization clauses can be constants, other fields, and call methods. Note: (1) the initialization clause must ensure that the type is correct; (2) the initialization clause cannot throw an exception. Fields that are not initialized are assigned the default initial value.
Object initialization. Initialization must be displayed. The initialization method is the constructor (called the constructor in the book) and the initialization section (in the class, except for fields, methods, nested classes, and nested interfaces, or use the code segment included in static {}, which is also called static initialization. Obviously, only static data members can be initialized ).
Use
Here, I understand that the methods in Java are passed by value without being passed by reference. The sample code for a test is as follows:
1 class A 2 { 3 int v = 0; 4 } 5 6 class B 7 { 8 public static void main(String[] args) 9 {10 A a = new A();11 System.out.println(a.v);12 foo(a);13 System.out.println(a.v);14 foo2(a);15 System.out.println(a.v);16 }17 18 public static void foo(A a)19 {20 a.v = 1;21 }22 23 public static void foo2(A a)24 {25 a = new A();26 a.v = 2;27 }28 }
If the output value is 0, 1, and 2 (displayed in three rows, the same below) according to the previous reference transmission viewpoint, the actual output value is 0, 1, and 1. This shows that the parameter A passed to foo2 is still a copy, which means passing by value.