Array
Programming
Coding Techniques
In-line Declaration
Syntax

Any way to declare an array in-line?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

“In-line array declaration” usually means creating and initializing an array at the exact place you need it instead of declaring it earlier and filling it later. Most mainstream languages support this, but the exact syntax depends on whether the array type is inferred, whether the language distinguishes arrays from lists, and whether you are declaring a variable or passing a temporary value into a method call.

The most useful distinction is between named array initialization and anonymous array creation used directly in an expression.

Inline Array Initialization in Common Languages

Here are a few common examples.

java
int[] numbers = {1, 2, 3, 4};
csharp
int[] numbers = new[] { 1, 2, 3, 4 };
javascript
const numbers = [1, 2, 3, 4];
python
numbers = [1, 2, 3, 4]

Although the idea is similar, not all of these are the same underlying data structure. Java and C# examples are fixed-type arrays. JavaScript and Python examples are dynamic list-like structures.

Passing an Array Directly to a Method

The more interesting question is often whether you can create the array on the call site without assigning it to a variable first.

In Java, yes, but the syntax depends on the context.

java
1import java.util.Arrays;
2
3public class InlineArrayCall {
4    static void printSum(int[] values) {
5        int sum = Arrays.stream(values).sum();
6        System.out.println(sum);
7    }
8
9    public static void main(String[] args) {
10        printSum(new int[] {1, 2, 3, 4});
11    }
12}

Notice that Java requires new int[] in that position. The shorter form with only braces works for variable initialization, but not for a standalone argument expression.

Java and C# Differences Matter

This is where many “why does this syntax fail” questions come from.

In Java, these two forms are different:

java
int[] a = {1, 2, 3};
java
int[] a = new int[] {1, 2, 3};

Both are valid for initialization, but only the second form can be used where an expression is required, such as a method argument or ternary branch.

C# has similar behavior, though type inference with new[] often makes call-site code shorter.

When Inline Arrays Improve Readability

Inline arrays are useful when:

  • the values are short and local
  • the array is only needed once
  • you want to keep test data next to the assertion or method call
  • a helper method expects a small fixed group of values

They are less useful when the literal is long or reused. In that case, give the array a name so the code explains intent instead of hiding a large block of values inside a call.

Inline Arrays Versus Varargs

Sometimes you do not need an inline array at all. If a method accepts varargs, the language builds the array for you.

java
1public class VarargsDemo {
2    static int sum(int... values) {
3        int total = 0;
4        for (int value : values) {
5            total += value;
6        }
7        return total;
8    }
9
10    public static void main(String[] args) {
11        System.out.println(sum(1, 2, 3, 4));
12    }
13}

This is often the cleanest call-site API when the method conceptually accepts a flexible number of arguments.

Common Pitfalls

  • Assuming the brace-only Java syntax works everywhere. As an expression, Java needs new int[] { ... }.
  • Treating Python lists and JavaScript arrays as identical to fixed-size typed arrays in Java or C#.
  • Using a huge inline literal inside a method call, which hurts readability more than it helps.
  • Forgetting that some APIs are better expressed as varargs instead of forcing callers to construct arrays.
  • Relying on inline initialization when the values should really be named constants or shared test fixtures.

Summary

  • Yes, most languages support inline array initialization, but the syntax is language-specific.
  • Java is strict about where the short brace-only form can be used.
  • Anonymous arrays are useful for one-off method calls and compact local data.
  • Varargs can sometimes remove the need for explicit inline arrays entirely.
  • Prefer the shortest syntax that still keeps the code readable and type-safe.

Course illustration
Course illustration

All Rights Reserved.