4

(I am studying for the Java Associate Exam OCJP 7)

A question asked to select examples of illegal initializations.

One of the answers was:-

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

The explanation said that when creating an anonymous array it was illegal to specify the size of the array.

From what I know this is NOT an anonymous array as it is named "k".

Calling a method such as :-

operateOnArray(new int[]{5,10});

Would have been an example of an anonymous array since it is not declared.

I can see that the "2" makes it illegal - but that does not make it anonymous,

Please can someone advise me?

3
  • The term anonymous applies to the class name, not to variables. Dec 3, 2014 at 14:42
  • Anonymous "Array" class? rather than variable "k"?
    – user3258396
    Dec 3, 2014 at 16:15
  • The terminology you are using seems to only be part of the test or practice material. It's not part of the Java Language Specification. But, yes, anonymous applies to type, not variables. Dec 3, 2014 at 16:17

4 Answers 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 :)

1
  • int [] k= new int[]{5,10}; So this is NOT anonymous?! Since it is assigned to the reference variable k which can be used later. Thanks for the link.
    – user3258396
    Dec 3, 2014 at 15:54
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...

0

It is not so clear what exactly is the question, but I get the point clearly that there is a confusion with regards how to correctly use the concept of an anonymous array in Java. You can only declare an anonymous array as an argument to a function. All else attempts to declare anonymous array not in this context, failed. This quick video shows it: anonymous array as argument to function

-1

If you see this part of the code:

new int[2]{5,10}

in the first declaration and then

new int[]{5,10}

from the second declaration. It's basically the same

So, a declaration like:

new int[]{5,10}

is an anonymous array, no matters what happen next. If you do not set it to a variable, it remains anonymous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.