Java Class Methods
Attributes give an object its data; methods give it behaviour. A method is a block of code inside a class that performs an action - it can use and change the object's attributes, accept inputs through parameters, and hand back a result. This lesson covers how to write methods, call them on an object, use the return value, and understand the difference between instance methods and static methods.
Adding a method to a class
A method is declared inside the class braces. Its signature has a return type (or 'void' if it returns nothing), a name, and a pair of parentheses that may hold parameters. Inside a method you can freely read and modify the object's own attributes, because the method belongs to the object.
A class with a method
public class Lamp {
boolean isOn;
void turnOn() {
isOn = true;
System.out.println("The lamp is now on.");
}
}Calling a method on an object
You call (or invoke) a method using the dot operator on an object, followed by the method name and parentheses. The method then runs against that particular object's data. Calling turnOn on one lamp switches on only that lamp.
Calling an instance method
public class Main {
public static void main(String[] args) {
Lamp desk = new Lamp();
desk.turnOn(); // The lamp is now on.
System.out.println(desk.isOn); // true
}
}Methods with parameters and return values
Methods become far more useful when they accept inputs and produce outputs. Parameters, listed inside the parentheses, let you pass information into the method. A return type other than void lets the method hand a result back to the caller with the 'return' keyword. The example below stores a running total and returns the new balance each time money is added.
Parameters and a return value
public class Wallet {
double balance;
double deposit(double amount) {
balance = balance + amount;
return balance; // hand the new balance back
}
}
public class Main {
public static void main(String[] args) {
Wallet w = new Wallet();
double updated = w.deposit(50.0);
System.out.println(updated); // 50.0
System.out.println(w.deposit(25.0)); // 75.0
}
}Instance methods vs static methods
The methods above are instance methods: they need an object to run, because they act on that object's data. A static method, marked with the 'static' keyword, belongs to the class itself rather than to any single object. You call it on the class name and it cannot touch instance attributes, because there is no specific object in play. Utility functions - like Math.max or a helper that squares a number - are typically static.
A static method
public class Calculator {
// no object needed - belongs to the class
static int square(int n) {
return n * n;
}
}
public class Main {
public static void main(String[] args) {
// called on the class, not on an object
System.out.println(Calculator.square(6)); // 36
}
}- A method is a named block of code inside a class that performs an action.
- Instance methods run on an object and can read and change its attributes.
- Parameters pass data in; the return type and 'return' keyword pass a result out.
- Static methods belong to the class, are called on the class name, and cannot use instance attributes.