This repository is a beginner-friendly guide to object-oriented programming (OOP) using Java.
OOP is a way of organizing programs around objects. An object groups:
- State — the data it stores
- Behavior — the actions it can perform
For example, a Car object might store its color and speed, and provide methods such as accelerate() and brake().
class Car {
private String color;
Car(String color) {
this.color = color;
}
void describe() {
System.out.println("This car is " + color + ".");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("blue");
myCar.describe();
}
}What is happening?
Caris a class: a blueprint for car objects.coloris a field: data stored by each car.Car(String color)is a constructor: it sets up a new object.describe()is a method: behavior the object provides.new Car("blue")creates an object (also called an instance).
Read the guides in this order:
OOP can make larger programs easier to understand by keeping related data and behavior together. It can also help you reuse code and change one part of a program without rewriting everything.
OOP is a tool, not a rule, so you use it when objects make the problem clearer.
Save an example in a file whose name matches its public class, then run:
javac Main.java
java MainYou need a Java Development Kit (JDK) installed. Java 17 or newer is a good choice for learning.