Object Creation and Storage · Création et Stockage d'Objets
| English | Français |
|---|---|
| constructor/kənˈstrʌktə/ | constructeur |
| instantiation/ˌɪnstænʃɪˈeɪʃn/ | instantiation |
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.
Créer un nouvel objet
- Pour créer un objet, utilisez le mot-clé
newet un constructeur 构造函数. Scanner sc = new Scanner(System.in);crée un nouveau objet Scanner.- L'instanciation 实例化 est l'action de créer un objet à partir d'une classe.
- Le constructeur initialise l'état de départ de l'objet.
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.
Paramètres du constructeur
- Un constructeur peut prendre des paramètres pour configurer le nouvel objet.
new Random(42)initialise un générateur aléatoire ;new String("hi")copie du texte.- Les paramètres correspondent aux paramètres du constructeur, par position et type.
- Des constructeurs différents (surchargés) permettent de construire la même classe de différentes manières.
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.
Stocker la référence
newretourne une référence vers l'objet ; stockez-la dans une variable du bon type.Scanner sc = new Scanner(System.in);→scdésormais pointe vers cet objet.- L'objet vit en mémoire ;
scn'est qu'un étiquette pointant vers lui. - Perdre la référence (aucune variable ne la retient) rend l'objet inaccessable.
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.
Deux variables, un seul objet
- Assigner une variable objet à une autre copie la référence, pas l'objet.
Scanner sc2 = sc;→ maintenantscetsc2pointent vers le même Scanner.- Une modification via
sc2est visible viasc— c'est un seul objet. - Ce comportement de copie de référence est une caractéristique (et un piège) des objets.
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.
new crée UN seul objet et retourne une référence vers celui-ci. Assigner cette variable à une autre (b = a;) copie la référence, donc les deux noms pointent vers le même objet — pas une seconde copie. Contrairement aux primitives (int y = x; copient la valeur). Oublier cela conduit à des surprises du type « J'ai modifié b, pourquoi a changé aussi ? ».
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.
Instanciation et aliasing :
Random r = new Random();→rpointe vers un nouvel objet Random.Random r2 = r;→r2pointe vers le même objet (pas de nouvel objet créé).- 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).
Créer un objet avec new plus un constructeur (instanciation) : new Scanner(System.in). new retourne une référence que vous stockez dans une variable. Assigner une variable objet à une autre copie la référence — les deux pointent alors vers le même objet (contrairement aux primitives, qui copient la valeur).
new makes one object; = copies the reference · new crée un objet ; = copie la référence
r and r2 share one object; x and y are independent copies. · r et r2 partagent un objet ; x et y sont des copies indépendantes.
Which keyword creates a new object? · Quel mot-clé crée un nouvel objet ?
new plus a constructor instantiates an object. · new plus un constructeur instancie un objet.
The special method called to build a new object is a ___ (one word). · La méthode spéciale appelée pour construire un nouvel objet est un ___ (un seul mot).
The constructor sets up the new object. · Le constructeur met en place le nouvel objet.
After Random r2 = r; how many Random objects exist? · Après Random r2 = r; combien d'objets Random existent ?
Assigning copies the reference, not the object — still one object. · L'affectation copie la référence, pas l'objet — toujours un seul objet.
Assigning one object variable to another (b = a) makes a second, independent object. · Affecter une variable d'objet à une autre (b = a) crée un second objet indépendant.
It copies the reference — both point to the same object. · Il copie la référence — les deux pointent vers le même objet.
For primitives, int y = x; copies the value, so x and y are independent. · Pour les primitifs, int y = x ; copie la valeur, donc x et y sont indépendants.
Primitives copy the value; objects copy the reference. · Les primitifs copient la valeur ; les objets copient la référence.