C#
JV

C# to Java

10 lessons

Progress0%
1Introduction2Type Systems3Properties & Getters4Generics5Collections6LINQ to Streams7Async Programming8Ecosystem9Modern Java Features10Concurrency
All Mirror Courses
C#
JV
Properties & Getters
MirrorLesson 3 of 10
Lesson 3

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.

Mirror Card
C#
From C#:

In C#, you're familiar with properties & getters.

JV
In Java:

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:

JV
Java 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#:

C#
C# (What you know)
class Person {
    public string Name { get; set; }
    public int Age { get; private set; }
}
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# auto-properties are one-liners; Java requires explicit getter/setter methods

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# {get; init;} creates immutable properties — no direct Java equivalent before records

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

Java Lombok @Data or @Getter/@Setter annotations can generate the boilerplate

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

Java records (Java 16+) are the closest equivalent to C# records with init-only properties

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

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.

C#
C#
public string FirstName { get; set; }
public string LastName { get; set; }
JV
Java
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; }
Rule of Thumb
Use your IDE's 'Generate > Getter and Setter' shortcut (Alt+Insert in IntelliJ) to avoid typing this boilerplate.

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.

C#
C#
public int Age { get; private set; }
public string Id { get; init; }
JV
Java
private final int age;  // final = no reassignment

public int getAge() { return age; }
// no setAge() = effectively read-only from outside

3. Computed Properties

C# properties with a custom getter body map to plain getter methods in Java.

C#
C#
public string FullName => $"{FirstName} {LastName}";
JV
Java
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.

C#
C#
record Person(string FirstName, string LastName);
// Person.FirstName, Person.LastName
JV
Java
record Person(String firstName, String lastName) {}
// person.firstName(), person.lastName()
// Note: accessor is a method call, not a property
Common Pitfall
Java record accessors use method-call syntax: person.firstName() — not person.firstName like C#.

Common 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
Common Pitfall
Don't assume Java works exactly like C#. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Java to practice these concepts.
PreviousNext