OOP-Grundlagen
Klassen und Objekte, Felder, Methoden, Konstruktoren, this und static - die Bauplan-und-Haus-Analogie.
Deutsche Übersetzung in Arbeit
Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.
Auf dieser Seite
Welcome to the idea that shapes almost all serious Java code: object-oriented programming (OOP). Instead of one long list of instructions, you model your program as a collection of objects that hold data and do things - just like the real world.
Classes and objects
A class is a blueprint. An object is a real thing built from that blueprint.
An architect draws one blueprint for a house (the class). From it, builders construct many actual houses (objects) - same design, but each with its own address, paint color, and occupants. One class, many objects.
Here's a class and objects made from it:
// The blueprint
public class Dog {
String name; // fields: data each Dog has
int age;
void bark() { // a method: something a Dog can do
System.out.println(name + " says: Woof!");
}
}// Building objects from the blueprint
Dog rex = new Dog();
rex.name = "Rex";
rex.age = 3;
Dog bella = new Dog();
bella.name = "Bella";
rex.bark(); // Rex says: Woof!
bella.bark(); // Bella says: Woof!The keyword new builds a new object. Each object keeps its own copy of the
fields - Rex and Bella have separate names.
Fields, methods, and this
- Fields are the data an object holds (
name,age). - Methods are the actions it can perform (
bark()). thisrefers to "the current object" - useful when a parameter has the same name as a field.
public class Dog {
String name;
void setName(String name) {
this.name = name; // this.name = the field; name = the parameter
}
}Constructors: building objects properly
Setting each field by hand is tedious and error-prone. A constructor is a special method that runs when an object is created, letting you set everything at once:
public class Dog {
String name;
int age;
// constructor: same name as the class, no return type
Dog(String name, int age) {
this.name = name;
this.age = age;
}
void bark() {
System.out.println(name + " says: Woof!");
}
}Dog rex = new Dog("Rex", 3); // clean and complete
rex.bark(); // Rex says: Woof!Constructors guarantee a valid object
By requiring values up front, a constructor ensures every Dog is created with a name and age - no half-built objects floating around. This is your first taste of writing safe, reliable code.
Static: belonging to the class, not the object
Normally, fields and methods belong to each object. A static member
belongs to the class itself - shared by all objects.
public class Dog {
static int count = 0; // shared across ALL dogs
String name;
Dog(String name) {
this.name = name;
count++; // every new Dog increases the shared count
}
}new Dog("Rex");
new Dog("Bella");
System.out.println(Dog.count); // 2 - accessed via the class, not an objectEach dog has its own name - that's an instance field. But "the total number of dogs in the world" belongs to the whole species, not any single dog
- that's a static field. You already use static all the time:
System.out.println-outis a static member of theSystemclass.
Encapsulation preview: keep fields private
Exposing fields directly (rex.age = -5) lets anyone put an object into a
nonsensical state. The OOP fix is to make fields private and control access
through methods:
public class BankAccount {
private double balance = 0; // hidden from outside
public void deposit(double amount) {
if (amount > 0) balance += amount; // guarded!
}
public double getBalance() {
return balance;
}
}Now nobody can secretly set a negative balance. You'll explore this principle - encapsulation - fully in the next lesson.
Quick check
What is the relationship between a class and an object?
Key takeaways
- A class is a blueprint; objects are instances built from it with the 'new' keyword.
- Fields hold an object's data; methods define its behavior; 'this' refers to the current object.
- Constructors run at creation time and ensure objects start in a valid state.
- static members belong to the class and are shared by all objects (e.g. a shared counter).
- Making fields private and exposing controlled methods (encapsulation) keeps objects valid.
Next, we'll explore the four pillars of OOP - the core principles that make object-oriented code powerful and maintainable.