Calling Class Methods · Chamando Métodos de Classe
| English | Português |
|---|---|
| class method/klæs ˈmeθəd/ | método de classe |
| 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.
Chamando um método de classe
- Um método de classe 类方法 (um método estático 静态方法) pertence à própria classe, não a um objeto.
- Chame-o com o nome da classe e um ponto:
Math.sqrt(16),Integer.parseInt("42"). - Nenhum objeto precisa existir — o método reside na classe.
- A palavra-chave
staticno cabeçalho marca um método como método de classe.
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().
Passando argumentos
- Coloque os argumentos dentro dos parênteses:
Math.max(3, 8). - Os valores são associados aos parâmetros do método, por posição.
- Forneça o número correto e os tipos adequados de argumentos, senão não compilará.
- Sem argumentos? Ainda assim escreva os parênteses vazios:
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.
Usando o valor retornado
- A maioria dos métodos de classe retorna um valor — capture-o em uma variável.
double r = Math.sqrt(16);armazena4.0emr.- Ignorar um valor retornado útil geralmente significa que você esqueceu de armazenar o resultado.
- Um valor retornado também pode ser alimentado diretamente em outra expressão.
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. instância — uma primeira visão
- Um método de classe (estático) é chamado na classe:
Math.abs(-5). - Um método de instância (próximas lições) é chamado em um objeto:
word.length(). - A diferença: há um ponto após um nome de classe ou após um objeto?
- Métodos de classe não precisam de um objeto; métodos de instância precisam.
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.
Chame um método de classe no NOME DA CLASSE, não num objeto: Math.sqrt(16), nunca m.sqrt(16). Combine o número e os tipos de argumentos com os parâmetros do método, e lembre-se de armazenar o valor retornado — Math.sqrt(16); sozinho calcula 4.0 e depois o descarta. Parênteses vazios ainda são necessários quando não há 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.
Combinando chamadas de métodos de classe:
int bigger = Math.max(3, 8);→biggerresulta em8.double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0.- Um valor retornado (
8) alimentado diretamente na próxima chamada.
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.
Um método de classe (estático) é chamado no nome da classe: Math.sqrt(16). Passe os argumentos corretos (associados por posição e tipo) entre parênteses, e armazene o valor retornado em uma variável para usá-lo. Isso difere de um método de instância, que é chamado em um objeto.
Calling a static method · Chamando um método estático
Math.max is called on the class and returns the larger value. · Math.max é chamado na classe e retorna o maior valor.
How do you correctly call the static method sqrt from the Math class? · Como você chama corretamente o método estático sqrt da classe Math?
Class methods are called on the class name with a dot. · Métodos de classe são chamados no nome da classe com um ponto.
What does Math.max(3, 8) return? · O que Math.max(3, 8) retorna?
max returns the larger of the two arguments. · max retorna o maior dos dois argumentos.
The keyword that marks a method as a class (static) method is... · A palavra-chave que marca um método como método de classe (estático) é...
static means the method belongs to the class, not an object. · static significa que o método pertence à classe, não a um objeto.
Math.sqrt(16); on its own line stores 4.0 for later use. · Math.sqrt(16); em sua própria linha armazena 4.0 para uso posterior.
It computes 4.0 but discards it — you must store the return value. · Ele calcula 4.0 mas descarta — você deve armazenar o valor retornado.
A class method is called on the class name, while an instance method is called on an object. · Um método de classe é chamado no nome da classe, enquanto um método de instância é chamado em um objeto.
Math.abs(-5) (class) vs word.length() (object). · Math.abs(-5) (classe) vs word.length() (objeto).