If you mean anonymous arrays like anonymous class where it was declared and instantiated at the same time without a name. The example below shows when you would use it.

For example, you have a method which expects an array of int as the argument in the parameter list.

public void fillPolygon(int[] xPoints, int[] yPoints, nPoints)

To invoke this method:

int[] xPoints = {1,2,3};
int[] yPoints = {4,5,6};
g.fillPolygon(xPoints, yPoints, 3);

You can also write it as:

g.fillPolygon(new int[]{1,2,3}, new int[]{4,5,6}, 3);

In what scenario anonymous arrays are used?

A: Use it when you are only going to use it once because you do not keep a reference (name) for the arrays.

Q: So what is the benefit of using "anonymous array"?

A: It keeps your codes concise.

Answer from user3437460 on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ anonymous-array-java
Anonymous Array in Java - GeeksforGeeks
April 22, 2022 - An array in Java without any name is known as an anonymous array. It is an array just for creating and using instantly.
Top answer
1 of 2
6

If you mean anonymous arrays like anonymous class where it was declared and instantiated at the same time without a name. The example below shows when you would use it.

For example, you have a method which expects an array of int as the argument in the parameter list.

public void fillPolygon(int[] xPoints, int[] yPoints, nPoints)

To invoke this method:

int[] xPoints = {1,2,3};
int[] yPoints = {4,5,6};
g.fillPolygon(xPoints, yPoints, 3);

You can also write it as:

g.fillPolygon(new int[]{1,2,3}, new int[]{4,5,6}, 3);

In what scenario anonymous arrays are used?

A: Use it when you are only going to use it once because you do not keep a reference (name) for the arrays.

Q: So what is the benefit of using "anonymous array"?

A: It keeps your codes concise.

2 of 2
3

Hello imagine that you have the following code:

public class Test { 
    public static void printArray(int []arr){
        for (int i : arr) {
            System.out.print(i);
        }
    }
    public static void main(String[] args) {

        int arr[]={1,2,3,4,5,6};
        printArray(arr);
    }
}

The output is 123456. If you want to call the printArrayMethod in a single line you have to use a anonymous array like the following:

printArray(new int[]{1,2,3,4,5,6});

You cant use something like the following:

printArray({1,2,3,4,5,6});

It is going to provoke a compilation fail.

And the question is, Why do I need to use an anonymous array and not a normal array like the first example.

Check the following code:

public class Test { 
    public static void printArray(int []arr){
        for (int i : arr) {
            System.out.print(i);
        }
    }
    public static void main(String[] args) {
        //Many code
        printArray(new int[]{1,2,3,4,5,6});
        //Many code
    }
}

If you use a normal array this array is going to alive in your heap during all the main method execution and with the anonymous array you are only use this array to the printArray method and after this the object is going to be eligible to the garbage collector.

๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 01 โ€บ anonymous-array-example-java-create.html
How to Create and Initialize Anonymous Array in Java? Example
Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesn't have any name you can not reuse it.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-is-an-anonymous-array-and-explain-with-an-example-in-java
What is an anonymous array and explain with an example in Java?
In addition to the above specified ways you can create an array without specifying any name such arrays are known as anonymous arrays. Since it doesnโ€™t have name to refer you can use it only once in your program.
๐ŸŒ
Medium
medium.com โ€บ @bhushanther123 โ€บ what-is-anonymous-array-in-java-b18e2d5a9a78
What is Anonymous Array in java ? - Bhushan Ther - Medium
August 26, 2022 - An array without any name is anonymous array in java. They are mainly created for one time use only. Note: Anonymous arrays can be passed as an argument of a method.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 682890 โ€บ java โ€บ anonymous-array
What is an anonymous array? (Beginning Java forum at Coderanch)
Since you are specifying the type of the array on the left side of the equal sign, Java already knows the type. And since you are specifying the initial values, it already knows the size. As a shortcut, Java lets you write this: int[] numbers2 = {42, 55, 99}; This approach is called an anonymous ...
๐ŸŒ
Quora
programmersworld.quora.com โ€บ Anonymous-Array-in-Java-With-Example-Know-Program
In Java, an Array without having any name is called an anonymous array. Using anonymous array we can pass an array with user values witho...
In Java, an Array without having any name is called an anonymous array. Using anonymous array we can pass an array with user values without the referenced variable.https://www.knowprogram.com/java/anonymous-array-java/
Find elsewhere
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ anonymous array in java with example
Anonymous Array in Java With Example - Know Program
March 28, 2021 - Syntax to create anonymous array in Java:- new <data type>[]{<list of values with comma separator>};
๐ŸŒ
Codeprime
codeprime.org โ€บ java-tutorials โ€บ arrays-in-java โ€บ anonymous-arrays-in-java
Anonymous Arrays in Java | CodePrime - Java Tutorials
An array without any name is called an anonymous array. Features of an anonymous array are: It has no name (obviously). It is created using the new keyword and pair of curly braces. It is created and initialized at the same time.
๐ŸŒ
The Crazy Programmer
thecrazyprogrammer.com โ€บ home โ€บ anonymous array in java
Anonymous Array in Java
July 19, 2015 - class AnonymousArray { static void print(int a[]) { for(int i=0;i<a.length;++i) System.out.print(a[i]+" "); } static void print(int a[][]) { for(int i=0;i<a.length;++i) { for(int j=0;j<a[i].length;++j) System.out.print(a[i][j]+" "); System.out.println(""); } } public static void main(String...s) { //1d anonymous array print(new int[]{10,20,30,40}); System.out.println("n"); //2d anonymous array print(new int[][]{{10,20},{30,40},{50,60}}); } }
๐ŸŒ
JavaByTechie
javabytechie.com โ€บ java โ€บ anonymous-array-in-java
Anonymous Array in Java with Example - javabytechie
March 26, 2023 - In Java, an anonymous array is an array that is created without explicitly assigning it to a variable.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ anonymous-array-in-java
Anonymous Array in Java - DataFlair
May 21, 2024 - class TestAn { void display(int a[],int s) { int f=0; for(int i=0;i<a.length;i++) { if(a[i]==s) { f=1; break; } } if(f==1) System.out.println("Searching success"); else System.out.println("Searching not success"); // int sum=0; // for(int i=0;i<a.length;i++) // { // System.out.println(a[i]); // sum=sum+a[i]; // } // System.out.println(sum); } } class TestAnonymous { public static void main(String args[]) { TestAn T=new TestAn(); T.display(new int[] {10,20,30,40,50},20); } }
๐ŸŒ
Wordpress
javarevisited.wordpress.com โ€บ 2025 โ€บ 07 โ€บ 29 โ€บ how-to-create-and-initialize-anonymous-array-in-java-example
How to Create and Initialize Anonymous Array in Java? Example | Java Prorgram Examples
July 29, 2025 - Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesnโ€™t have any nโ€ฆ
๐ŸŒ
YouTube
youtube.com โ€บ satish c j
Java Programming - Introduction to Anonymous Arrays - Returning Anonymous Arrays - CSE1007 - YouTube
Code can downloaded from https://codespindle.com/Java/Java_Anonymous_Arrays.htmlIn this video we are going to see an Introduction to Anonymous arrays. How to...
Published: July 20, 2020
Views: 3K
๐ŸŒ
Medium
medium.com โ€บ @hasibulhimu49 โ€บ variable-arguments-with-anonymous-arrays-in-java-4d2b12838fd7
Variable Arguments with Anonymous Arrays in Java | by Mohammad Hasibul Hasan | Medium
February 15, 2025 - public class Example { static void ... a name } } Use varargs (...) for flexible method arguments. Use an anonymous array when passing a predefined list of values....
๐ŸŒ
Blogger
javawebtutorial.blogspot.com โ€บ 2013 โ€บ 10 โ€บ anonymous-array-in-java.html
Anonymous Array in Java ~ Java Tutorial
January 3, 2018 - 2> We cannot use + operator with the array reference for indicating the next member of the array because java do not deals with the address at the user level. int arr [ ] = new int [ 4 ] ; ( arr + 1 ) ; // Invalid in Java 3> Cannot assign direct a string value to a array reference .
Top answer
1 of 4
3

Mmmm... from what I've been taught (and also from what I've read), the definition of an Anonymous Array is: "Array without any name, it is an array just for create and use it". And since it does not have any name, you should not be able to reuse that array.

The best references I had when I prepared for the OCJP was:

anonymous int array : new int[] { 1, 2, 3, 4};
anonymous String array : new String[] {"one", "two", "three"};
anonymous char array :  new char[] {'a', 'b', 'c');

You can notice that (and also you already know) these type of arrays have the creation and initialization at the same time (as you initialize them in the same line you create them using the new() keyword without assigning to any variable and you would not be able to reuse it later).

So, from what you have mentioned, when you assign an array to a variable, even when you create it and initialize at the same line, it is being assigned to a variable so it is able to be reused later, it is not anonymous, it can be referenced, so I wonder why in the examples of "anonymous arrays" you see something like:

int [] k= new int[2]{5,10};

Anonymous means "not known by name" which is not the case in the line you specified since the array is assigned to a variable called "k". However, this one shows the property of anonymous array-object creation which is pointed by a reference variable "k", so if we write just "new int[]{1,2,3};" then this is how anonymous array-object can be created...

When I prepared for OCJP 7 I read many articles from this page, I would recommend it to you to go deeply in Java in the future :)

Reference: http://javarevisited.blogspot.com/2012/01/anonymous-array-example-java-create.html

You can find also good examples and articles there :)

2 of 4
0

I think it will be like an anonymous method...

The anonymous array would be like myListView.setAdapter(this, new String [] {"Peter", "Paul", "Marry"});

Where the (new String [] {"Peter", "Paul", "Marry"}) is the anonymous array :-)

So a normal (not anonymous) array would be decleared (maybe in onCreate) like

String [] array = new String []...

While the anonymous one won't be decleared before usage...

Top answer
1 of 2
1

Remember that new Object() {...} gives you a value not a type.

So, here is how you could create an array of instances of anonymous classes:

final Object[] anonArray = new Object[]{
     new Object(){
        public String foo = "bar1";
     }, 
     new Object(){
        public String foo = "bar2";
     },
     new Object(){
        public String foo = "bar3";
     },
     new Object(){
        public String foo = "bar4";
     }
};

Or if you want an array with the same anonymous class instance 4 times:

final Object anonInstance = new Object(){
    public String foo = "bar1";
};
final Object[] anonArray = new Object[]{
     anonInstance, anonInstance, anonInstance, anonInstance
};

Or if you want 4 distinct instances of the same anonymous class:

final Object[] anonArray = new Object[4];
for (int i = 0; i < anonArray.length; i++) {

    // Just to show we can do this ....
    final String bar = "bar" + 1;

    anonArray[i] = new Object(){
        public String foo = bar;  // Look!
    };
}

Note that in all of the above the array type is Object[] not "array of the anonymous subclass of Object". It is difficult to conceive that this would matter, but if it did matter you would need to resort to reflection to create the array class. (See @ernest_k's answer.)

Or just declare an array of an appropriate custom class or interface, and create anon subclasses from that class / interface. Anonymous subclasses of Object are not exactly practical ...

2 of 2
1

The syntax new Object() {} creates an instance of the anonymous class. It does not offer a way to declare an array of the anonymous class.

This should really be discouraged, but if you need to have your anonymous class as the array's component type, you can use java.lang.reflect.Array:

Object o = new Object() {
    public String foo = "bar";
};

Object[] anonArray = (Object[]) java.lang.reflect.Array.newInstance(o.getClass(), 4);
anonArray[0] = o;

That is probably going to have a hard time passing code review (you can't construct other elements without reflection). So, an alternative: declare a local class in your method:

class Anon {
    public String foo = "bar";
};

Anon[] anonArray = new Anon[4];

The above Anon is local to the method (it's an underused feature of the language)