---
description: "Array of Objects in Java: Learn how to create, Initialize and use an array of objects with step by step code examples and explanation."
title: Array of Objects in Java
image: https://www.guru99.com/images/array-of-objects-in-java.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Array of Objects in Java stores object references in a fixed-size contiguous slot, letting a single variable hold many instances of a class and access each one through an index for iteration, mutation, and passing to methods.

- 📦 **Reference storage:** Each slot in a Java array of objects holds a reference to an object, not the object itself, similar to any object variable in Java.
- 🧱 **Two-step creation:** Allocate the array with new ClassName\[size], then allocate each object with new ClassName() before calling methods on it.
- ⚠️ **NullPointerException:** Every slot starts as null. Calling a method on an uninitialized slot throws NullPointerException at runtime.
- 🔁 **Iteration:** Use a for loop with array length or an enhanced for loop to walk every object in the array.
- 📏 **Fixed size:** An array cannot grow after allocation. Use ArrayList when the number of objects changes at runtime.
- 🏗️ **Constructor init:** Constructors passed inline during creation give shorter, safer setup than post-hoc setter calls.
- 🚚 **Method parameter:** Passing the array to a method transfers a reference, so changes inside the method are visible to the caller.

Read More

[![Array of Objects in Java](https://www.guru99.com/images/array-of-objects-in-java.png)](https://www.guru99.com/images/array-of-objects-in-java.png)

## What is an Array of Objects in Java?

A Java **array of objects** stores multiple object instances of the same class in a single fixed-size container. Unlike a primitive array that stores values such as `int`, `double`, or `boolean` directly in each slot, an array of objects stores *references*. Every slot points to the memory location of an object, and the object itself lives on the Java heap.

This layout matches how any object [variable](https://www.guru99.com/java-variables.html) in Java works. The array simply groups several object references together so you can access them by index. It is the natural choice when the number of objects is known upfront and does not change during the program’s lifetime.

### Syntax

```
ClassName[] obj = new ClassName[array_length];
```

The right side allocates space for `array_length` references, all initialised to `null`. No object of `ClassName` exists yet, only the container that can hold references to such objects.

## How to Create an Array of Objects in Java

Creating a working array of objects always takes two allocation steps. The first allocates the reference container, and the second allocates each object that the container will point to. Skipping the second step is the single most common source of `NullPointerException` in beginner Java code.

**Step 1)** Open your [code editor](https://www.guru99.com/try-java-editor.html) and copy the following code:

```
class ObjectArray {
    public static void main(String args[]) {
        Account obj[] = new Account[2];
        // obj[0] = new Account();
        // obj[1] = new Account();
        obj[0].setData(1, 2);
        obj[1].setData(3, 4);
        System.out.println("For Array Element 0");
        obj[0].showData();
        System.out.println("For Array Element 1");
        obj[1].showData();
    }
}

class Account {
    int a;
    int b;

    public void setData(int c, int d) {
        a = c;
        b = d;
    }

    public void showData() {
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
    }
}
```

**Step 2)** Save the file as `ObjectArray.java`, then compile and run it.

**Step 3)** Note the error. Try to debug it before continuing to Step 4.

**Step 4)** Inspect the line `Account obj[] = new Account[2];`. It allocates an array of two reference variables of type `Account`, but it does not allocate any `Account` objects. Both slots hold `null`, and calling `obj[0].setData(...)` on a `null` reference throws a `NullPointerException` at runtime.

[![Java Array of Objects reference variables]()](https://www.guru99.com/images/uploads/2012/07/java-array-of-objects.jpg)

**Step 5)** Uncomment lines 4 and 5. These two lines create real `Account` objects and assign them to `obj[0]` and `obj[1]`. The array now holds real references, and the code runs cleanly.

[![Java Array of Objects populated]()](https://www.guru99.com/images/uploads/2012/07/java-array-of-objects-1.jpg)

**Output:**

```
For Array Element 0
Value of a = 1
Value of b = 2
For Array Element 1
Value of a = 3
Value of b = 4
```

## How to Initialize an Array of Objects in Java

Beyond the setter-based initialisation shown above, an array of objects can be initialised in three cleaner ways. Each style trades verbosity for clarity, so pick the one that best matches the situation.

1. **Loop initialisation** — allocate the array, then walk each index and assign a fresh object. This is the standard pattern for large arrays or when the object count is dynamic.
2. **Constructor initialisation** — pass values directly to the constructor at creation time so no separate setter call is needed.
3. **Array literal initialisation** — declare and populate in a single statement with an inline `{ ... }` initialiser, ideal for small fixed sets.

```
class Account {
    int a, b;

    public Account(int a, int b) {   // constructor
        this.a = a;
        this.b = b;
    }
}

public class InitDemo {
    public static void main(String[] args) {

        // 1) Loop initialisation
        Account[] loopArr = new Account[3];
        for (int i = 0; i < loopArr.length; i++) {
            loopArr[i] = new Account(i, i * 10);
        }

        // 2) Constructor initialisation, one slot at a time
        Account[] ctorArr = new Account[2];
        ctorArr[0] = new Account(1, 2);
        ctorArr[1] = new Account(3, 4);

        // 3) Array literal initialisation
        Account[] litArr = {
            new Account(5, 6),
            new Account(7, 8),
            new Account(9, 10)
        };

        System.out.println("loop size   = " + loopArr.length);
        System.out.println("ctor size   = " + ctorArr.length);
        System.out.println("literal size= " + litArr.length);
    }
}
```

Loop initialisation is the safest default because it guarantees no slot is left `null`. Constructor initialisation makes intent explicit when each object needs different arguments, and array literals keep small demo code compact. Do not mix styles inside the same array unless you have a reason: consistency makes bugs easier to spot.

**Don't Miss:**

- [Java Swing Tutorial: How to Create a GUI Application in Java](https://www.guru99.com/java-swing-gui.html)
- [Java Tutorial for Beginners: Complete Guide](https://www.guru99.com/java-tutorial.html)
- [Scala Tutorial](https://www.guru99.com/scala-tutorial.html)
- [Selection Sorting in Java Program with Example](https://www.guru99.com/selection-sorting-java.html)

## Iterating and Accessing Elements in a Java Array of Objects

Once an array of objects is populated, three patterns cover almost every access need. Choose the loop style that matches how much control the code needs over the index.

**Classic `for` loop** gives access to the index, which is useful for replacing elements, comparing neighbours, or iterating a subrange.

```
for (int i = 0; i < obj.length; i++) {
    System.out.println("Element " + i);
    obj[i].showData();
}
```

**Enhanced `for` loop** (the “for-each” loop) hides the index and reads more naturally when the index is not needed:

```
for (Account acc : obj) {
    acc.showData();
}
```

**Streams (Java 8 and later)** convert the array into a pipeline, which is handy for filtering, mapping, or aggregating without writing a loop:

```
import java.util.Arrays;

Arrays.stream(obj)
      .filter(acc -> acc.a > 0)
      .forEach(Account::showData);
```

To read or update a single element, use direct index access, for example `obj[1].setData(10, 20)`. Always check the length first with `obj.length` to avoid `ArrayIndexOutOfBoundsException`. When any slot could be `null`, guard the access with a null check or an `Optional` to keep the program running instead of crashing.

## Array of Objects vs ArrayList of Objects in Java

A plain array of objects is fast and predictable, but its fixed size becomes a limitation as soon as the object count changes at runtime. Java’s Collections framework provides `ArrayList` as the dynamic alternative. The table below highlights when each choice is appropriate.

| Feature | Array of Objects | ArrayList of Objects |
| --- | --- | --- |
| Size | Fixed at creation | Grows and shrinks automatically |
| Declaration | `Account[] a = new Account[5];` | `List<Account> list = new ArrayList<>();` |
| Access element | `a[i]` | `list.get(i)` |
| Add element | Not supported after creation | `list.add(obj)` |
| Type safety | Compile-time typed by declaration | Generics enforced at compile time |
| Performance | Marginally faster indexed reads | Amortised O(1) add, some overhead per element |
| Preferred when | Size is known and fixed | Size is unknown or changes at runtime |

Use an array when a tight memory footprint and predictable indexing matter, for example in matrix layouts or performance-critical hot paths. Reach for `ArrayList` in most everyday application code, because the ability to add and remove without reallocation outweighs the small per-element overhead.

## FAQs

📦 What is an array of objects in Java?

An array of objects in Java is a fixed-size container that stores references to instances of the same class. Each slot points to an object on the heap, and elements are accessed by zero-based index using square brackets.

🧱 How do you declare and initialise an array of objects in Java?

Declare the array with ClassName\[] arr = new ClassName\[size], then create each object with new ClassName() in a loop or with an inline array literal. Both steps are required; the first allocates only reference slots.

⚠️ Why does my array of objects throw NullPointerException?

Because new ClassName\[size] only allocates the array itself. Every slot starts as null, so calling any method on that slot throws NullPointerException. Fix it by assigning a new ClassName() to each index before use.

🆚 What is the difference between an array of objects and an ArrayList?

An array has a fixed size chosen at creation, while ArrayList grows and shrinks automatically. Arrays give slightly faster indexed reads; ArrayList adds convenient add, remove, and stream methods for everyday application code.

🔁 How do I loop through an array of objects in Java?

Use a classic for loop with arr.length when you need the index, or an enhanced for-each loop for simple iteration. For filtering or transformation, wrap the array with Arrays.stream(arr) and chain the pipeline you need.

🚚 How do I pass an array of objects to a method?

Declare the parameter as ClassName\[] name, then call the method with the array variable. Java passes the array reference by value, so mutations to element fields inside the method are visible to the caller.

🤖 How does AI use arrays of objects when generating Java code?

Machine learning code generators recognise the two-step allocation pattern and complete the missing new ClassName() lines that beginners forget. They also suggest ArrayList when the target collection size is unknown, reducing NullPointerException bugs.

🛠️ Can GitHub Copilot scaffold arrays of objects for me?

Yes. GitHub Copilot generates array declarations, constructor loops, iteration blocks, and ArrayList conversions from a comment describing the domain model. Review generated code for correct constructor arguments and safe null handling before use.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/array-of-objects-in-java.png","url":"https://www.guru99.com/images/array-of-objects-in-java.png","width":"700","height":"250","caption":"Array of Objects in Java","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/array-of-objects.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/array-of-objects.html","name":"Array of Objects in Java"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/array-of-objects.html#webpage","url":"https://www.guru99.com/array-of-objects.html","name":"Array of Objects in Java","dateModified":"2026-10-01T12:40:00+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/array-of-objects-in-java.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/array-of-objects.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"Array of Objects in Java","description":"Array of Objects in Java: Learn how to create, Initialize and use an array of objects with step by step code examples and explanation.","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-01T12:40:00+05:30","image":{"@id":"https://www.guru99.com/images/array-of-objects-in-java.png"},"copyrightYear":"2026","name":"Array of Objects in Java","subjectOf":[{"@type":"HowTo","name":"How to Create Array Of Objects in Java?","description":"Here is a step by step process on how to create array of objects:","step":[{"@type":"HowToStep","name":"Step 1) Open your code editor.","text":"Copy the following code into an editor.","url":"https://www.guru99.com/array-of-objects.html#step1"},{"@type":"HowToStep","name":"Step 2) Save your code.","text":"Save, Compile &amp; Run the Code.","url":"https://www.guru99.com/array-of-objects.html#step2"},{"@type":"HowToStep","name":"Step 3) Error=?","text":"Try and debug before proceeding to step 4.","url":"https://www.guru99.com/array-of-objects.html#step3"},{"@type":"HowToStep","name":"Step 4) Check Account obj[] = new Account[2]","text":"The line of code, Account obj[] = new Account[2]; exactly creates an array of two reference variables as shown below.","image":"https://www.guru99.com/images/uploads/2012/07/java-array-of-objects.jpg","url":"https://www.guru99.com/array-of-objects.html#step4"},{"@type":"HowToStep","name":"Step 5) Uncomment Line.","text":"Uncomment Line # 4 &amp; 5. This step creates objects and assigns them to the reference variable array as shown below. Your code must run now.","image":"https://www.guru99.com/images/uploads/2012/07/java-array-of-objects-1.jpg","url":"https://www.guru99.com/array-of-objects.html#step5"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is an array of objects in Java?","acceptedAnswer":{"@type":"Answer","text":"An array of objects in Java is a fixed-size container that stores references to instances of the same class. Each slot points to an object on the heap, and elements are accessed by zero-based index using square brackets."}},{"@type":"Question","name":"How do you declare and initialise an array of objects in Java?","acceptedAnswer":{"@type":"Answer","text":"Declare the array with ClassName[] arr = new ClassName[size], then create each object with new ClassName() in a loop or with an inline array literal. Both steps are required; the first allocates only reference slots."}},{"@type":"Question","name":"Why does my array of objects throw NullPointerException?","acceptedAnswer":{"@type":"Answer","text":"Because new ClassName[size] only allocates the array itself. Every slot starts as null, so calling any method on that slot throws NullPointerException. Fix it by assigning a new ClassName() to each index before use."}},{"@type":"Question","name":"What is the difference between an array of objects and an ArrayList?","acceptedAnswer":{"@type":"Answer","text":"An array has a fixed size chosen at creation, while ArrayList grows and shrinks automatically. Arrays give slightly faster indexed reads; ArrayList adds convenient add, remove, and stream methods for everyday application code."}},{"@type":"Question","name":"How do I loop through an array of objects in Java?","acceptedAnswer":{"@type":"Answer","text":"Use a classic for loop with arr.length when you need the index, or an enhanced for-each loop for simple iteration. For filtering or transformation, wrap the array with Arrays.stream(arr) and chain the pipeline you need."}},{"@type":"Question","name":"How do I pass an array of objects to a method?","acceptedAnswer":{"@type":"Answer","text":"Declare the parameter as ClassName[] name, then call the method with the array variable. Java passes the array reference by value, so mutations to element fields inside the method are visible to the caller."}},{"@type":"Question","name":"How does AI use arrays of objects when generating Java code?","acceptedAnswer":{"@type":"Answer","text":"Machine learning code generators recognise the two-step allocation pattern and complete the missing new ClassName() lines that beginners forget. They also suggest ArrayList when the target collection size is unknown, reducing NullPointerException bugs."}},{"@type":"Question","name":"Can GitHub Copilot scaffold arrays of objects for me?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot generates array declarations, constructor loops, iteration blocks, and ArrayList conversions from a comment describing the domain model. Review generated code for correct constructor arguments and safe null handling before use."}}]}],"@id":"https://www.guru99.com/array-of-objects.html#schema-24380","isPartOf":{"@id":"https://www.guru99.com/array-of-objects.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/array-of-objects.html#webpage"}}]}
```
