Skip to main content
Published on

Properties in Java

Share:

Introduction

In Java, encapsulation is one of the fundamental pillars of object-oriented programming. Through properties, using getter and setter methods, this pillar is put into practice. These functions allow fine-grained control over how an object's data is accessed and modified.

Why Use Properties?

1. Control over data

It is possible to validate or modify data before it is stored or returned.

2. Flexibility

Internal changes to getter and setter methods can be made without altering the way an object is used externally.

3. Security

Limits direct access to data fields, ensuring their integrity.

How to Implement

A common implementation involves declaring a private field with public methods to get and set the values.

public class Example {
  private String name;

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

  // Setter
  public void setName(String name) {
    if(name != null && !name.trim().isEmpty()) {
      this.name = name;
    } else {
      throw new IllegalArgumentException("Invalid name");
    }
  }
}

Best Practices When Using Properties

  1. Data Validation: Always validate data in the setter method before assigning it to the field.
  2. Consistent Encapsulation: Avoid exposing fields directly. Use getters and setters.
  3. Documentation: Document the purpose and constraints of each property.
  4. Convenient Naming: Use consistent naming conventions for properties. Typically, methods starting with "get" and "set" are used.

Practical Example

Let's look at an example where we implement a Properties class representing a person with a first name, last name, and age.

package com.caffeinealgorithm.programaremjava;

public class Properties {
  private String firstName = "", lastName = "";
  private int age = 28;

  public void setFirstName(String firstName) {
    if (firstName != "")
      this.firstName = firstName;
    else
      System.out.println("The string for the first name cannot be empty.");
  }

  public void setLastName(String lastName) {
    if (lastName != "")
      this.lastName = lastName;
    else
      System.out.println("The string for the last name cannot be empty.");
  }

  public int getAge() {
    return age;
  }

  public void information() {
    System.out.printf("Name: %s %s\n", firstName, lastName);
  }
}

Additional Tips

  • Immutability: In certain scenarios, it can be beneficial to create read-only properties, reinforcing the concept of immutability.
  • Performance: Be careful when implementing complex logic in getters and setters, as this can impact performance.
  • Computed Properties: Beyond simply returning a field, getters can be used to calculate and return a value based on multiple fields.

Conclusion

Properties, when used correctly, enrich the code, making it secure, flexible, and easy to maintain. In Java, getter and setter methods are the standard way to implement this feature. By mastering them, you ensure robustness and clarity in your applications.

Happy coding!