Java Bean Class
Java Bean Class
😊 Let's learn what a Java Bean class is in a very simple and clear way — with
example, features, and real-life analogy.
📦 Real-Life Analogy
Imagine a "Student Form":
📝 Fields: Name, Age, ID
👩💻 You can write into the form → (setters)
👀 You can read from the form → (getters)
A Java Bean is like this form: it stores data safely and gives access through methods.
public class Student { // 1. Private fields private String name; private int age; // 2.
No-arg constructor public Student() { } // 3. Getters public String getName() { return
name; } public int getAge() { return age; } // 4. Setters public void setName(String name)
{ this.name = name; } public void setAge(int age) { this.age = age; } }
🛠 How to Use the Bean
java Copy Edit
public class Main { public static void main(String[] args) { Student s = new Student();
s.setName("Naziya"); s.setAge(22); System.out.println(s.getName()); // Naziya
System.out.println(s.getAge()); // 22 } }
📋 Summary
Feature
Class access
Fields
Constructor
Access methods
Would you like to see how a Java Bean is used in a Spring Boot project, like for saving student data
from Postman to database? I’d love to show you! 😊