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.
The above is the detailed content of What is encapsulation and how to use it.. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software