Calling Class Methods · Llamar a Métodos de Clase
| English | Español |
|---|---|
| class method/klæs ˈmeθəd/ | método de clase |
| static method/ˈstætɪk ˈmeθəd/ | método estático |
Calling a class method
- A class method 类方法 (a static method 静态方法) belongs to the class itself, not to an object.
- Call it with the class name and a dot:
Math.sqrt(16),Integer.parseInt("42"). - No object needs to exist — the method lives on the class.
- The keyword
staticin the header marks a method as a class method.
Llamada a un método de clase
- Un método de clase (un método estático) pertenece a la propia clase, no a un objeto.
- Llámalo con el nombre de la clase y un punto:
Math.sqrt(16),Integer.parseInt("42"). - No se necesita que exista ningún objeto; el método reside en la clase.
- La palabra clave
staticen el encabezado marca un método como método de clase.
Passing arguments
- Put the arguments inside the parentheses:
Math.max(3, 8). - The values are matched to the method's parameters, by position.
- Give the right number and types of arguments, or it won't compile.
- No arguments? Still write the empty parentheses:
sc.nextLine().
Pasa de argumentos
- Coloca los argumentos dentro de los paréntesis:
Math.max(3, 8). - Los valores se emparejan con los parámetros del método por posición.
- Proporciona el número y los tipos correctos de argumentos, o no compilará.
- ¿Sin argumentos? Still escribe los paréntesis vacíos:
sc.nextLine().
Using the return value
- Most class methods return a value — capture it in a variable.
double r = Math.sqrt(16);stores4.0inr.- Ignoring a useful return value usually means you forgot to store the result.
- A returned value can also feed straight into another expression.
Uso del valor de retorno
- La mayoría de los métodos de clase devuelven un valor; cáptalo en una variable.
double r = Math.sqrt(16);almacena4.0enr.- Ignorar un valor de retorno útil generalmente significa que olvidaste almacenar el resultado.
- Un valor devuelto también puede alimentarse directamente en otra expresión.
Static vs. instance — a first look
- A class (static) method is called on the class:
Math.abs(-5). - An instance method (next lessons) is called on an object:
word.length(). - The tell: is there a dot after a class name or after an object?
- Class methods don't need an object; instance methods do.
Estático vs. instancia — una primera mirada
- Un método de clase (estático) se llama sobre la clase:
Math.abs(-5). - Un método de instancia (lecciones siguientes) se llama sobre un objeto:
word.length(). - La clave: ¿hay un punto después de un nombre de clase o después de un objeto?
- Los métodos de clase no necesitan un objeto; los métodos de instancia sí lo necesitan.
Call a class method on the CLASS name, not an object: Math.sqrt(16), never m.sqrt(16). Match the argument count and types to the method's parameters, and remember to store the return value — Math.sqrt(16); on its own computes 4.0 and then throws it away. Empty parentheses are still required when there are no arguments.
Llama a un método de clase usando el nombre de la CLASE, no de un objeto: Math.sqrt(16), nunca m.sqrt(16). Ajusta el conteo y los tipos de argumentos a los parámetros del método, y recuerda almacenar el valor de retorno: Math.sqrt(16); por sí solo calcula 4.0 y luego lo descarta. Los paréntesis vacíos son requeridos incluso cuando no hay argumentos.
Combining class method calls:
int bigger = Math.max(3, 8);→biggeris8.double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0.- A return value (
8) fed straight into the next call.
Combinación de llamadas a métodos de clase:
int bigger = Math.max(3, 8);→biggeres8.double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0.- Un valor de retorno (
8) alimentado directamente en la siguiente llamada.
A class (static) method is called on the class name: Math.sqrt(16). Pass the right arguments (matched by position and type) in parentheses, and store the return value in a variable to use it. This differs from an instance method, which is called on an object.
Un método de clase (estático) se llama sobre el nombre de la clase: Math.sqrt(16). Pasa los argumentos correctos (emparejados por posición y tipo) entre paréntesis, y almacena el valor de retorno en una variable para usarlo. Esto difiere de un método de instancia, que se llama sobre un objeto.
Calling a static method · Llamar un método estático
Math.max is called on the class and returns the larger value. · Math.max se llama en la clase y devuelve el valor mayor.
How do you correctly call the static method sqrt from the Math class? · ¿Cómo se llama correctamente el método estático sqrt de la clase Math?
Class methods are called on the class name with a dot. · Los métodos de clase se llaman con el nombre de la clase seguido de un punto.
What does Math.max(3, 8) return? · ¿Qué devuelve Math.max(3, 8)?
max returns the larger of the two arguments. · max devuelve el mayor de los dos argumentos.
The keyword that marks a method as a class (static) method is... · La palabra clave que marca un método como un método de clase (estático) es...
static means the method belongs to the class, not an object. · static significa que el método pertenece a la clase, no a un objeto.
Math.sqrt(16); on its own line stores 4.0 for later use. · Math.sqrt(16); en una línea separada almacena 4.0 para su uso posterior.
It computes 4.0 but discards it — you must store the return value. · Calcula 4.0 pero lo descarta — debes almacenar el valor devuelto.
A class method is called on the class name, while an instance method is called on an object. · Un método de clase se llama con el nombre de la clase, mientras que un método de instancia se llama en un objeto.
Math.abs(-5) (class) vs word.length() (object). · Math.abs(-5) (clase) vs word.length() (objeto).