Java Class Methods
A class bundles data (its fields) with the code that works on that data (its methods). Methods without static are instance methods: they are called on an object and can use that object's fields directly. Methods with static belong to the class and work without any object, like the methods you wrote in Methods.
Instance methods
An instance method can read and change the fields of the object it is called on:
class Rectangle {
double width, height;
double area() {
return width * height;
}
double perimeter() {
return 2 * (width + height);
}
void scale(double factor) {
width *= factor;
height *= factor;
}
}
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle();
r.width = 3;
r.height = 4;
System.out.println("Area: " + r.area());
System.out.println("Perimeter: " + r.perimeter());
r.scale(2);
System.out.println("Area after scaling: " + r.area());
}
}Area: 12.0 Perimeter: 14.0 Area after scaling: 48.0
Inside area(), width means "the width of the rectangle this method was called on". If you had two rectangles, r1.area() and r2.area() would use different values.
toString: how an object prints itself
When you print an object, Java calls its toString() method. Without your own version it prints the class name and a code. Write a toString method to get a readable result:
class Point {
int x, y;
public String toString() {
return "(" + x + ", " + y + ")";
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
p.x = 3;
p.y = -2;
System.out.println(p);
System.out.println("Point p is at " + p);
}
}(3, -2) Point p is at (3, -2)
toString must be public; the reason is explained in Inheritance. From now on, most classes in this tutorial will have one.
Static methods in a class
A static method does not belong to any object, so it cannot use instance fields. It is the right choice for helpers that only need their parameters, like Math.max:
class Temperature {
static double toFahrenheit(double celsius) {
return celsius * 9 / 5 + 32;
}
static double toCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Temperature.toFahrenheit(37));
System.out.println(Temperature.toCelsius(212));
}
}98.6 100.0
You call a static method through the class name: Temperature.toFahrenheit(37). No new is needed.
Methods that work together
Methods can call other methods of the same object. Here deposit and withdraw both use show:
class Wallet {
double money;
void deposit(double amount) {
money += amount;
show("Deposited " + amount);
}
boolean withdraw(double amount) {
if (amount > money) {
show("Not enough money for " + amount);
return false;
}
money -= amount;
show("Withdrew " + amount);
return true;
}
void show(String action) {
System.out.println(action + ", balance " + money);
}
}
public class Main {
public static void main(String[] args) {
Wallet w = new Wallet();
w.deposit(500);
w.withdraw(200);
w.withdraw(1000);
}
}Deposited 500.0, balance 500.0 Withdrew 200.0, balance 300.0 Not enough money for 1000.0, balance 300.0
Use an instance method when the work depends on an object's data, and a static method when it does not.
Practice exercises
- Add a method
isSquare()toRectanglethat returns true when width and height are equal. - Write a class
Studentwith anameand an array ofmarks, and a methodaverage(). - Give
StudentatoStringmethod that prints the name and the average.