Properties & Getters
Properties & Getters
Introduction
In this lesson, you'll learn about properties & getters in Java. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with properties & getters.
Java has its own approach to properties & getters, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
class Person {
private String name;
public String getName() { return name; }
public void setName(String n) { name = n; }
private int age;
public int getAge() { return age; }
}Comparing to C#
Here's how you might have written similar code in C#:
class Person {
public string Name { get; set; }
public int Age { get; private set; }
}You may be used to different syntax or behavior.
C# auto-properties are one-liners; Java requires explicit getter/setter methods
You may be used to different syntax or behavior.
C# {get; init;} creates immutable properties — no direct Java equivalent before records
You may be used to different syntax or behavior.
Java Lombok @Data or @Getter/@Setter annotations can generate the boilerplate
You may be used to different syntax or behavior.
Java records (Java 16+) are the closest equivalent to C# records with init-only properties
You may be used to different syntax or behavior.
IntelliJ and other IDEs can auto-generate getters/setters from fields
Step-by-Step Breakdown
1. Auto-Properties vs Getters/Setters
C# auto-properties hide the backing field. Java requires you to write the field plus explicit getter and setter methods.
public string FirstName { get; set; }
public string LastName { get; set; }private String firstName;
private String lastName;
public String getFirstName() { return firstName; }
public void setFirstName(String v) { firstName = v; }
public String getLastName() { return lastName; }
public void setLastName(String v) { lastName = v; }2. Read-Only Properties
C# {get; private set;} or {get; init;} creates read-only properties. In Java, omit the setter and make the field final.
public int Age { get; private set; }
public string Id { get; init; }private final int age; // final = no reassignment
public int getAge() { return age; }
// no setAge() = effectively read-only from outside3. Computed Properties
C# properties with a custom getter body map to plain getter methods in Java.
public string FullName => $"{FirstName} {LastName}";public String getFullName() {
return firstName + " " + lastName;
}4. Java Records (Java 16+)
Java records are the closest equivalent to C# records — immutable data carriers with auto-generated accessors, equals, hashCode, and toString.
record Person(string FirstName, string LastName);
// Person.FirstName, Person.LastNamerecord Person(String firstName, String lastName) {}
// person.firstName(), person.lastName()
// Note: accessor is a method call, not a propertyCommon Mistakes
When coming from C#, developers often make these mistakes:
- C# auto-properties are one-liners; Java requires explicit getter/setter methods
- C# {get; init;} creates immutable properties — no direct Java equivalent before records
- Java Lombok @Data or @Getter/@Setter annotations can generate the boilerplate
Key Takeaways
- Java has no property syntax — write explicit getName/setName methods
- Use final fields + no setter for read-only access
- Java records (16+) replace C# init-only records for immutable data
- IDE generators eliminate the boilerplate in practice