day-32: Getter and Setter Methods in Java - Simple Example

Getters and Setters in Java Getters and setters are methods used to access (get) and modify (set) private variables in a class. They are a key part of encapsulation, which hides the internal state of an object and allows controlled access through methods. Why Use Getters & Setters? ✔ Control access to private variables ✔ Add validation before setting values ✔ Make code more maintainable Example: Person Class with Getter & Setter public class Jewellery_shop { private int gold_rate; // Getter method (must return gold_rate) public int getGoldRate() { return gold_rate; } // Setter method public void setGoldRate(int gold_rate) { this.gold_rate = gold_rate; System.out.println("Gold Rate set to: " + gold_rate); } } public class Customer { public static void main(String[] args) { Jewellery_shop js = new Jewellery_shop(); System.out.println("Initial Gold Rate: " + js.getGoldRate()); // Set new rate js.setGoldRate(90); // Print updated rate System.out.println("Updated Gold Rate: " + js.getGoldRate()); } } Best Practices / Rules for Getters and Setters: ✅ Naming Convention: Getter: getVariableName() Setter: setVariableName(Type value) ✅ Return Type: Getter: Should return the variable (not void) Setter: Should be void and accept one parameter ✅ Encapsulation Rule: Keep your variables private Access them via public getters and setters ---------------- Doc Asssite by AI -------------------------------

Apr 23, 2025 - 04:29
 0
day-32: Getter and Setter Methods in Java - Simple Example

Getters and Setters in Java
Getters and setters are methods used to access (get) and modify (set) private variables in a class. They are a key part of encapsulation, which hides the internal state of an object and allows controlled access through methods.

Why Use Getters & Setters?

✔ Control access to private variables
✔ Add validation before setting values
✔ Make code more maintainable

Example: Person Class with Getter & Setter

public class Jewellery_shop {

    private int gold_rate;

    // Getter method (must return gold_rate)
    public int getGoldRate() {
        return gold_rate;
    }

    // Setter method
    public void setGoldRate(int gold_rate) {
        this.gold_rate = gold_rate;
        System.out.println("Gold Rate set to: " + gold_rate);
    }
}


public class Customer {

    public static void main(String[] args) {
        Jewellery_shop js = new Jewellery_shop();

        System.out.println("Initial Gold Rate: " + js.getGoldRate());

        // Set new rate
        js.setGoldRate(90);

        // Print updated rate
        System.out.println("Updated Gold Rate: " + js.getGoldRate());
    }
}

Best Practices / Rules for Getters and Setters:
✅ Naming Convention:
Getter: getVariableName()
Setter: setVariableName(Type value)

✅ Return Type:
Getter: Should return the variable (not void)
Setter: Should be void and accept one parameter

✅ Encapsulation Rule:
Keep your variables private
Access them via public getters and setters

---------------- Doc Asssite by AI -------------------------------