Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungMCP Tool Poisoning: A Name Allowlist Is Not Enough(22.09.2026 um 18:56 Uhr)
Sichere ProgrammierungGiggleGigs: A Job Board That Actually Uses AI Where It Helps(22.09.2026 um 18:57 Uhr)
Sichere ProgrammierungThe Bootstrapped SaaS Flywheel: How to Hit $10k MRR Without VC Capital(22.09.2026 um 19:05 Uhr)
Sichere ProgrammierungThe container was running software nobody built(22.09.2026 um 19:08 Uhr)
Sichere ProgrammierungYour Terminal Tasks, Everywhere: Introducing Tasku Cloud(22.09.2026 um 19:08 Uhr)
Sichere ProgrammierungMCP Tool Poisoning: A Name Allowlist Is Not Enough(22.09.2026 um 18:56 Uhr)
Sichere ProgrammierungGiggleGigs: A Job Board That Actually Uses AI Where It Helps(22.09.2026 um 18:57 Uhr)
Sichere ProgrammierungThe Bootstrapped SaaS Flywheel: How to Hit $10k MRR Without VC Capital(22.09.2026 um 19:05 Uhr)
Sichere ProgrammierungThe container was running software nobody built(22.09.2026 um 19:08 Uhr)
Sichere ProgrammierungYour Terminal Tasks, Everywhere: Introducing Tasku Cloud(22.09.2026 um 19:08 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

What is encapsulation and how to use it.

What is encapsulation? Encapsulation in Java is all about keeping the details of how something works hidden while still letting others use it. You group your data (like variables) and methods (like functions) into one unit, called a class.…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

What is encapsulation?

Encapsulation in Java is all about keeping the details of how something works hidden while still letting others use it. You group your data (like variables) and methods (like functions) into one unit, called a class. Instead of letting everyone directly access your data, you provide methods (getters and setters) to control how it’s accessed or changed. This way, you protect your data and keep your code clean and organized, without letting anyone mess with the inner workings unless you want them to.



How to use it

To use encapsulation in Java, you create a class with private fields and provide public methods (like getters and setters) to access and modify those fields. This ensures that the data can only be changed in a controlled way. For example, if you want to create a Person class where the name and age are private, you would use getters to retrieve the values and setters to update them. Here's how you can do it:




public class Person {
// Private fields
private String name;
private int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Getter for name
public String getName() {
return name;
}

// Setter for name
public void setName(String name) {
this.name = name;
}

// Getter for age
public int getAge() {
return age;
}

// Setter for age
public void setAge(int age) {
if(age > 0) { // Simple validation
this.age = age;
} else {
System.out.println("Age must be positive.");
}
}
}

// Using the Person class
public class Main {
public static void main(String[] args) {
Person person = new Person("John", 25);

// Accessing fields using getters
System.out.println(person.getName()); // Output: John
System.out.println(person.getAge()); // Output: 25

// Modifying fields using setters
person.setName("Jane");
person.setAge(30);

System.out.println(person.getName()); // Output: Jane
System.out.println(person.getAge()); // Output: 30
}
}







Lets break it down

Let's break down the code and explain each section step by step:






1. Class Definition with Private Fields





public class Person {
// Private fields
private String name;
private int age;
}










Explanation:


This is the Person class where we define two private fields: name (a String) and age (an int). By making these fields private, we ensure that no other class can directly access or modify them. This is the core idea of encapsulation — hiding the internal state of an object.






2. Constructor





    // Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}










Explanation:


The constructor initializes the Person object when it's created. It takes two parameters, name and age, and assigns these values to the private fields. This ensures that when a new Person object is created, it starts with valid data.






3. Getter and Setter for





    // Getter for name
public String getName() {
return name;
}

// Setter for name
public void setName(String name) {
this.name = name;
}










Explanation:


The constructor initializes the Person object when it's created. It takes two parameters, name and age, and assigns these values to the private fields. This ensures that when a new Person object is created, it starts with valid data.






4. Getter and Setter for age (with validation)





    // Getter for age
public int getAge() {
return age;
}

// Setter for age
public void setAge(int age) {
if (age > 0) { // Simple validation
this.age = age;
} else {
System.out.println("Age must be positive.");
}
}










Explanation:


The getter getAge() works the same way as the one for name, allowing access to the age field.

The setter setAge() not only allows modification of the age field but also adds a validation check. The if condition ensures that the age is only set if it's a positive number. If an invalid age is provided (like a negative number), the setter prevents the update and prints a message instead. This is an example of how encapsulation lets you control what kind of data can be set.






5. Using the Person Class





// Using the Person class
public class Main {
public static void main(String[] args) {
Person person = new Person("John", 25);

// Accessing fields using getters
System.out.println(person.getName()); // Output: John
System.out.println(person.getAge()); // Output: 25

// Modifying fields using setters
person.setName("Jane");
person.setAge(30);

System.out.println(person.getName()); // Output: Jane
System.out.println(person.getAge()); // Output: 30
}
}










Explanation:


In this section, we create a Person object (person) using the constructor. The constructor sets the initial name as "John" and age as 25.



Then, we use the getters (getName() and getAge()) to print the values of name and age. After that, we use the setters (setName() and setAge()) to update the values. Finally, the updated values are printed using the getters again.



Key Points of Encapsulation in Action:








  • Data Protection:


    The private fields can't be directly accessed or modified from outside the class.







  • Controlled Access:


    Getters and setters provide a controlled way of accessing and modifying the private fields.







  • Validation:


    The setter for age demonstrates how you can enforce rules (e.g., age must be positive) to protect the data from invalid input.



Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten What is encapsulation and how to use it.

Thematisch verwandte Begriffe: What, encapsulation · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-75517 | Novu provides an API for sending notifications through multiple channels…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick