OOP: Klassen und Objekte in Java: Ausführlicher Leitfaden
What is it and why? (a real-life analogy)
Imagine you decided to build a city from LEGO bricks. You have a box of parts, but no instructions. You can build a house, but you'll have to rebuild each next house from scratch, picking out the pieces. This is like programming without OOP — each time you write code from scratch, even if it's very similar to the previous one.
Now imagine you have a house blueprint. This blueprint specifies: the house has walls, a roof, windows, and a door. The blueprint is a class. And when you take this blueprint and build a real house from it — that's an object. You can build 10, 100, or 1000 identical houses from one blueprint. Moreover, you can take the house blueprint and add a garage to it — this is called inheritance.
In programming, it's exactly the same. A class is a blueprint, template, or form for creating objects. An object is a specific instance created from that template. Each object has a state (data, fields) and behavior (methods, actions).
💡 Important to remember: A class is a data type that you invent yourself. Just like Java has the
inttype for integers orStringfor strings, you can create your own type — for example,Car,Student, orBankAccount.
Why is this needed? Imagine you're writing a program for a library. Without OOP, you'd have to store the book title in one array, the author in another, and the publication year in a third. But with OOP, you simply create a Book class, and each book is a single object that already contains everything you need. Convenient, right?
Theory: how it works internally
Let's look under the hood of Java and understand what happens in the computer's memory when we work with classes and objects.
Memory: Stack and Heap
When a program starts, Java divides the RAM into two main areas:
- Stack — stores primitive types (
int,double,boolean) and references to objects. It works on the "last in, first out" principle. Very fast, but small. - Heap — the objects themselves live here. Lots of space, but working with it is a bit slower.
When you write Book myBook = new Book();, the following happens:
- A variable
myBookis created on the stack (this is just a reference, like an address on an envelope). - Memory for the new
Bookobject is allocated in the heap. - The constructor is called, which initializes the object's fields.
- A reference to this object (the address in the heap) is written into the variable
myBook.
Object lifecycle
- Declaration:
Book myBook;— just saying that such a variable will exist. - Creation:
new Book();— allocating memory. - Initialization: the constructor fills the fields with initial values.
- Usage: we call methods, change fields.
- Destruction: when the object is no longer needed, Java itself removes it from memory (this is called garbage collection).
⚠️ Important: In Java, you cannot delete an object from memory yourself. The Garbage Collector does this for you when it sees that there are no more references to the object.
What is a constructor?
A constructor is a special method that is called when an object is created. It has no return type, and its name matches the class name. A constructor is needed to set the initial state of an object.
If you don't write any constructor, Java adds a default constructor — an empty one, without parameters. But as soon as you create your own constructor, the "default" one disappears.
Syntax in Java: all forms and variations
Let's break down the syntax of classes in Java from simple to complex. Here is the full structure of a class:
// Access modifiers: public, private, protected, default
// Keyword class
// Class name (capitalized)
public class Dog {
// 1. FIELDS (object state)
// Can be primitive or reference types
private String name; // dog's name
private int age; // age
private String breed; // breed
private boolean isHungry; // is it hungry?
// 2. CONSTRUCTORS
// Default constructor (if you don't write your own)
public Dog() {
this.name = "Bobik";
this.age = 1;
this.breed = "Mutt";
this.isHungry = true;
}
// Constructor with parameters
public Dog(String name, int age, String breed) {
this.name = name; // this.name — class field, name — parameter
this.age = age;
this.breed = breed;
this.isHungry = true; // by default the dog is hungry
}
// 3. METHODS (object behavior)
// Getters (get field value)
public String getName() {
return name;
}
// Setters (set field value)
public void setName(String name) {
this.name = name;
}
// Regular methods
public void bark() {
System.out.println(name + " says: Woof-woof!");
}
public void feed() {
if (isHungry) {
System.out.println(name + " eats with pleasure!");
isHungry = false;
} else {
System.out.println(name + " is not hungry.");
}
}
// 4. STATIC FIELDS AND METHODS (belong to the class, not the object)
public static int dogCount = 0; // how many dogs have been created in total
public static void showDogCount() {
Syst