Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
What you will learn
- Write an instance method that changes only the object it's called on
- Hide fields with private and read and write them through a getter and a setter
- Override toString so println shows useful text instead of Car@1b6d3586
- Generate getters, setters and toString with IntelliJ's Generate menu
Before you start
- Java constructors and the this keyword
Fields hold an object's data. Methods make it do something. This article gives a Car class three kinds of method: an instance method that drives it, a getter and setter around a private field, and toString, which decides how the car prints.
The Car class
package org.studyeasy.s03l03;
// A car that drives itself, keeps its numbers private and can describe itself
public class Car {
private String brand;
private int speed;
Car(String brand) {
this.brand = brand;
}
// Instance method: it works on THIS car's own speed
public void accelerate(int kmh) {
speed = speed + kmh;
}
// Getter: hands the value out
public int getSpeed() {
return speed;
}
// Setter: the one way to write a new value in
public void setSpeed(int speed) {
this.speed = speed;
}
// toString: how a Car looks when it is printed
@Override
public String toString() {
return brand + " doing " + speed + " km/h";
}
}
Instance methods: each car keeps its own speed
An object is also called an instance, which is why methods like accelerate are called instance methods. Each call runs on one object and changes only that object's fields.
package org.studyeasy.s03l03;
public class Ex01_CallMethods {
public static void main(String[] args) {
Car taxi = new Car("Toyota");
Car bus = new Car("Volvo");
taxi.accelerate(40); // runs on the taxi only
taxi.accelerate(20); // 40 + 20
bus.accelerate(30); // the bus keeps its own speed
System.out.println("Taxi: " + taxi.getSpeed() + " km/h");
System.out.println("Bus: " + bus.getSpeed() + " km/h");
}
}
Taxi: 60 km/h
Bus: 30 km/h
The pattern is object, dot, method: taxi.accelerate(40). getSpeed() is a getter: it hands a field's value back. Call taxi.accelerate() with no number and it won't compile; the method needs the km/h.
private fields, getters and setters
private hides a field from every other class. Outside code has to go through methods: the getter reads, the setter writes. setSpeed receives a value and stores it with this.speed = speed, the same field-versus-parameter pattern as in constructors.
package org.studyeasy.s03l03;
public class Ex02_GettersSetters {
public static void main(String[] args) {
Car taxi = new Car("Toyota");
taxi.setSpeed(80); // write through the setter
System.out.println("Speed: " + taxi.getSpeed());
taxi.setSpeed(50); // overwrite it
System.out.println("Slowed to: " + taxi.getSpeed());
}
}
Speed: 80
Slowed to: 50
Try to reach into the field directly with taxi.speed = 80; and the compiler says speed has private access in Car. Set it with the setter and read it with the getter. Why bother? Because the object then guards its own data; you'll see that properly in access modifiers and encapsulation.
toString: tell Java how a car prints
package org.studyeasy.s03l03;
public class Ex03_PrintACar {
public static void main(String[] args) {
Car taxi = new Car("Toyota");
taxi.accelerate(60);
System.out.println(taxi); // calls toString()
String report = "Now: " + taxi; // so does + with text
System.out.println(report);
}
}
Toyota doing 60 km/h
Now: Toyota doing 60 km/h
println always asks the object for its text, and toString is where the object answers. Joining text with + calls it too. Without your own toString, Java falls back to its default: the class name, an @ and a code, something like Car@1b6d3586, which tells you nothing. @Override marks that you're replacing Java's own version.
Let IntelliJ write them
You'll rarely type these by hand. In IntelliJ, press Alt+Insert (Windows and Linux) or Cmd+N (Mac) to open the Generate menu. Pick Getter and Setter, tick the fields and press OK, or pick toString() and edit the text it returns. Generating is fine; knowing what the generated code does is the part you can't skip.
Recap
| Piece | Job |
|---|---|
| Instance method | works on the one object it's called on |
| Getter | hands a field's value back |
| Setter | puts a new value into a field |
private | hides a field from every other class |
toString | the text println shows for the object |
Fields private, methods public: the object looks after its own data.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
Java Packages and Imports: Every Class Has an Address
Two classes called Order, one in shop and one in shipping: the package keeps them apart. How the package line maps to folders, plain and static imports, using both Orders in one file, and the clash when you import both.
