What are the key differences between static implementations in C# versus Java?

I’ve been working through Effective Java by Josh Bloch and came across his builder pattern implementation. The pattern works great in Java but I’m running into issues when trying to convert it to C#. Here’s my working Java version:

public class Vehicle {
    private String model;
    private String brand;
    private int capacity = 0;

    public static void main(String[] args) {
        Vehicle newVehicle = new Vehicle.Creator("Test Model").Brand("Test Brand").Capacity(5).construct();
    }

    private Vehicle(Creator creator) {
        this.model = creator.model;
        this.brand = creator.brand;
        this.capacity = creator.capacity;
    }

    public static class Creator {
        private String model;
        private String brand;
        private int capacity = 0;

        public Creator(String model) {
            this.model = model;
        }

        public Creator Brand(String brand) {
            this.brand = brand;
            return this;
        }

        public Creator Capacity(int capacity) {
            this.capacity = capacity;
            return this;
        }

        public Vehicle construct() {
            return new Vehicle(this);
        }
    }
}

When I try to port this to C#:

public class Vehicle
{
    private String model;
    private String brand;

    private Vehicle(Creator creator)
    {
        model = creator.model;
        brand = creator.brand;
    }

    public static class Creator
    {
        private String model;
        private String brand;
        private int capacity = 0;

        public Creator(String val)
        {
            this.model = val;
        }

        public Creator Brand(String val)
        {
            this.brand = val;
            return this;
        }

        public Creator Capacity(int val)
        {
            this.capacity = val;
            return this;
        }

        public Vehicle construct()
        {
            return new Vehicle(this);
        }
    }
}

I get compiler errors saying “cannot declare instance members in a static class”. What am I missing here? Can this builder approach work in C# the same way it does in Java? Also wondering about thread safety with this approach.

yeah, c# has stricter rules for static classes than java. in java, static nested classes can have instances, but c# won’t permit instance fields or constructors in them. just remove the static keyword from creator and it should work fine after that.

The problem is C# handles static classes completely differently than Java’s static nested classes. I’ve dealt with this migration issue before - C# static classes are just utility containers that can’t hold instance state or be instantiated at all. Your Java code works because Java lets you instantiate static nested classes independently from their outer class while keeping them accessible without an outer instance reference. Fix is simple: remove the static modifier from your Creator class in C#. You’ll get the same accessibility and functionality as Java. I’ve used this pattern in several C# projects and the non-static nested class works exactly like Java’s behavior, just with slightly different syntax.

You’re hitting a classic C#/Java difference. In C#, static classes can’t have instance members or constructors. Java’s static nested classes work differently - the static just means they don’t need a reference to the outer class, but you can still instantiate them normally. Just remove static from your Creator class declaration and it’ll compile fine. The functionality stays exactly the same as your Java version. As for thread safety - this builder pattern is actually thread-safe by design. Each builder instance gets created and used in a single thread, then spits out a new immutable object. No shared mutable state between threads. But if you’re planning to reuse builder instances across threads, you’ll need extra synchronization.