0% found this document useful (0 votes)
0 views2 pages

Java Bean Class

A Java Bean is a simple Java class designed to store and transfer data, adhering to specific rules such as being public, having a no-argument constructor, private fields, and public getter/setter methods. An analogy is provided comparing a Java Bean to a 'Student Form' that allows data input and retrieval. Java Beans are commonly used in web applications and frameworks like Spring Boot for efficient data handling.

Uploaded by

sthaheer974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Java Bean Class

A Java Bean is a simple Java class designed to store and transfer data, adhering to specific rules such as being public, having a no-argument constructor, private fields, and public getter/setter methods. An analogy is provided comparing a Java Bean to a 'Student Form' that allows data input and retrieval. Java Beans are commonly used in web applications and frameworks like Spring Boot for efficient data handling.

Uploaded by

sthaheer974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Great question, Naziya!

😊 Let's learn what a Java Bean class is in a very simple and clear way — with
example, features, and real-life analogy.

🫘 What is a Java Bean Class?


A Java Bean is just a simple Java class that follows some special rules.
It is mostly used to store and transfer data (like a box to carry data).

✅ Java Bean Rules


A Java Bean must:
1. Be a public class
2. Have a no-argument constructor
3. Have private variables (fields)
4. Have public getter and setter methods

📦 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.

💡 Java Bean Example


java Copy Edit

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 } }

🤔 Why Use Java Beans?


✅ To transfer data in web apps (between layers)
✅ Used in Spring Boot for @RequestBody and @Entity
✅ Works well with tools, frameworks, and libraries

📋 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! 😊

You might also like