Object Creation and Storage · Creación y Almacenamiento de Objetos
| English | Español |
|---|---|
| constructor/kənˈstrʌktə/ | constructor |
| instantiation/ˌɪnstænʃɪˈeɪʃn/ | instantiación |
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.
Creando un nuevo objeto
- Para crear un objeto, utiliza la palabra clave
newy un constructor. Scanner sc = new Scanner(System.in);crea un objeto Scanner nuevo.- La instanciación es el acto de crear un objeto a partir de una clase.
- El constructor establece el estado inicial del objeto.
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.
Argumentos del constructor
- Un constructor puede recibir argumentos para configurar el nuevo objeto.
new Random(42)establece una semilla en un generador aleatorio;new String("hi")copia texto.- Los argumentos coinciden con los parámetros del constructor, por posición y tipo.
- Diferentes constructores (sobrecargados) permiten construir la misma clase de distintas formas.
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.
Almacenando la referencia
newdevuelve una referencia al objeto; guárdala en una variable del tipo adecuado.Scanner sc = new Scanner(System.in);—scahora hace referencia a ese objeto.- El objeto reside en memoria;
sces simplemente la etiqueta que apunta a él. - Si pierdes la referencia (ninguna variable la retiene), el objeto se vuelve inaccesible.
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.
Dos variables, un objeto
- Asignar una variable de objeto a otra copia la referencia, no el objeto.
Scanner sc2 = sc;— ahorascysc2apuntan al mismo Scanner.- Un cambio hecho a través de
sc2es visible a través desc, ya que es un solo objeto. - Este comportamiento de copia de referencias es una característica definitoria (y trampa) de los objetos.
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 crea UN objeto y devuelve una referencia a él. Asignar esa variable a otra (b = a;) copia la referencia, por lo que ambos nombres apuntan al mismo objeto, no a una segunda copia. Contrasta esto con las primitivas (int y = x; copian el valor). Olvidar esto lleva a sorpresas del tipo "He cambiado b, ¿por qué cambió a también?".
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.
Instanciación y aliasing:
Random r = new Random();→rhace referencia a un nuevo objeto Random.Random r2 = r;→r2apunta al mismo objeto (no se creó un objeto nuevo).- Ahora tanto
rcomor2comparten el estado de un solo generador.
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).
Crea un objeto con new más un constructor (instantiación): new Scanner(System.in). new devuelve una referencia que almacenas en una variable. Asignar una variable de objeto a otra copia la referencia; ambas pasarán a referirse al mismo objeto (a diferencia de las primitivas, que copian el valor).
new makes one object; = copies the reference · new crea un objeto; = copia la referencia
r and r2 share one object; x and y are independent copies. · r y r2 comparten un objeto; x e y son copias independientes.
Which keyword creates a new object? · ¿Qué palabra clave crea un nuevo objeto?
new plus a constructor instantiates an object. · new más un constructor instancian un objeto.
The special method called to build a new object is a ___ (one word). · El método especial llamado para construir un nuevo objeto es un ___ (una palabra).
The constructor sets up the new object. · El constructor configura el nuevo objeto.
After Random r2 = r; how many Random objects exist? · Después de Random r2 = r; ¿cuántos objetos Random existen?
Assigning copies the reference, not the object — still one object. · Asignar copia la referencia, no el objeto — todavía hay un solo objeto.
Assigning one object variable to another (b = a) makes a second, independent object. · Asignar una variable de objeto a otra (b = a) crea un segundo objeto independiente.
It copies the reference — both point to the same object. · Copia la referencia — ambas apuntan al mismo objeto.
For primitives, int y = x; copies the value, so x and y are independent. · Para primitivos, int y = x; copia el valor, por lo que x e y son independientes.
Primitives copy the value; objects copy the reference. · Los primitivos copian el valor; los objetos copian la referencia.