Tạo và Lưu trữ Đối tượng
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| constructor/kənˈstrʌktə/ | Hàm khởi tạo |
| instantiation/ˌɪnstænʃɪˈeɪʃn/ | Khởi tạo thực thể |
Making a new object
- To create an object, use the
newkeyword and a constructor 构造函数. Scanner sc = new Scanner(System.in);builds a fresh Scanner object.- Instantiation 实例化 is the act of creating an object from a class.
- The constructor sets up the object's starting state.
Constructor arguments
- A constructor can take arguments to configure the new object.
new Random(42)seeds a random generator;new String("hi")copies text.- The arguments match the constructor's parameters, by position and type.
- Different constructors (overloaded) let you build the same class different ways.

A primitive variable holds its value; a reference variable holds an arrow to an object
Storing the reference
newreturns a reference to the object; store it in a variable of the right type.Scanner sc = new Scanner(System.in);—scnow refers to that object.- The object lives in memory;
scis just the label pointing at it. - Lose the reference (no variable holds it) and the object becomes unreachable.
Two variables, one object
- Assigning one object variable to another copies the reference, not the object.
Scanner sc2 = sc;— nowscandsc2point to the same Scanner.- A change through
sc2is visible throughsc— it's one object. - This reference-copy behavior is a defining feature (and trap) of objects.
new creates ONE object and hands back a reference to it. Assigning that variable to another (b = a;) copies the reference, so both names point to the same object — not a second copy. Contrast primitives (int y = x; copies the value). Forgetting this leads to "I changed b, why did a change too?" surprises.
Instantiation and aliasing:
Random r = new Random();→rrefers to a new Random object.Random r2 = r;→r2points at the same object (no new object made).- Both
randr2now share one generator's state.
Create an object with new plus a constructor (instantiation): new Scanner(System.in). new returns a reference you store in a variable. Assigning one object variable to another copies the reference — both then refer to the same object (unlike primitives, which copy the value).
new tạo ra một đối tượng; = sao chép tham chiếu
r và r2 chia sẻ một đối tượng; x và y là các bản sao độc lập.
Từ khóa nào tạo ra một đối tượng mới?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
new cộng với constructor khởi tạo một đối tượng.
Phương thức đặc biệt được gọi để xây dựng một đối tượng mới là ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Constructor thiết lập đối tượng mới.
Sau khi Random r2 = r; có bao nhiêu đối tượng Random tồn tại?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Gán sao chép tham chiếu, không phải đối tượng — vẫn chỉ có một đối tượng.
Gán một biến đối tượng cho biến khác (b = a) tạo ra một đối tượng thứ hai độc lập.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Nó sao chép tham chiếu — cả hai trỏ đến cùng một đối tượng.
Đối với kiểu nguyên thủy, int y = x; sao chép giá trị, nên x và y độc lập.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Kiểu nguyên thủy sao chép giá trị; đối tượng sao chép tham chiếu.