fixed-size arrays
programming tutorial
object-oriented programming
data structures
coding guide

How to create a fixed-size array of objects

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A fixed-size array of objects means the number of slots is chosen when the array is created and does not change afterward. What varies by language is whether those slots hold actual objects immediately or just references that still need object instances assigned to them.

The Important Distinction: Slots Versus Instances

In most object-oriented languages, creating an array of objects creates storage for references, not automatically initialized objects for every position.

That means there are two separate steps:

  • allocate the array with a fixed length
  • create and assign objects into its elements

This distinction surprises beginners because an array can have length 10 while still containing 10 null references.

Example in Java

java
1class Person {
2    String name;
3
4    Person(String name) {
5        this.name = name;
6    }
7}
8
9public class Main {
10    public static void main(String[] args) {
11        Person[] people = new Person[3];
12        people[0] = new Person("Ana");
13        people[1] = new Person("Ben");
14        people[2] = new Person("Cara");
15
16        for (Person p : people) {
17            System.out.println(p.name);
18        }
19    }
20}

new Person[3] creates three fixed slots. It does not create three Person objects by itself.

Example in C#

csharp
1using System;
2
3class Person
4{
5    public string Name { get; }
6    public Person(string name) => Name = name;
7}
8
9class Program
10{
11    static void Main()
12    {
13        Person[] people = new Person[3];
14        people[0] = new Person("Ana");
15        people[1] = new Person("Ben");
16        people[2] = new Person("Cara");
17
18        foreach (var person in people)
19        {
20            Console.WriteLine(person.Name);
21        }
22    }
23}

Again, the array length is fixed, but the object instances must be assigned separately.

Initializing in One Expression

If you already know the objects up front, most languages let you create the fixed-size array and its initial contents together.

Java:

java
1Person[] people = {
2    new Person("Ana"),
3    new Person("Ben"),
4    new Person("Cara")
5};

C#:

csharp
1Person[] people = {
2    new Person("Ana"),
3    new Person("Ben"),
4    new Person("Cara")
5};

This is often the cleanest style when the contents are known at initialization time.

Fixed Size Means Length, Not Immutability

A fixed-size array does not mean the objects inside it are immutable, and it does not mean array elements cannot be replaced.

It means:

  • the number of slots cannot change

It does not mean:

  • the objects cannot mutate
  • an element cannot be reassigned

For example, you can still overwrite people[1] with a different Person as long as the array length stays 3.

When an Array Is the Right Tool

Use a fixed-size array when:

  • the size is known up front
  • indexed access matters
  • you want minimal overhead
  • resizing is not needed

If the collection must grow or shrink, a dynamic structure such as List<T> or ArrayList is usually more appropriate.

Pre-Populating All Slots

If you want every slot initialized automatically, do it explicitly in a loop.

java
1Person[] people = new Person[3];
2for (int i = 0; i < people.length; i++) {
3    people[i] = new Person("Person " + i);
4}

The same principle applies in C#:

csharp
1Person[] people = new Person[3];
2for (int i = 0; i < people.Length; i++)
3{
4    people[i] = new Person($"Person {i}");
5}

This is the right approach when the number of objects is fixed but their contents are generated programmatically.

Common Pitfalls

The biggest mistake is assuming new Person[5] creates five Person instances. It only creates five slots.

Another mistake is using an array when the collection size is expected to change frequently.

A third issue is confusing fixed size with immutability. The array length is fixed, but the element values may still change.

Finally, trying to access members on an uninitialized slot leads to null-reference errors in languages such as Java and C#.

Summary

  • A fixed-size array has a fixed number of slots after creation.
  • In many languages, object arrays store references, not automatically created objects.
  • Allocate the array first, then fill the elements with actual object instances.
  • Use direct initializer syntax when the contents are known up front.
  • Fixed size does not mean immutable contents.
  • Choose arrays when length is known and stable; choose dynamic collections when it is not.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.