0

I am studying for the Java 7 OCA Exam and I have a doubt about array initializing. Why is there a compilation error here:

Object[] obj = new Object[]{ "aaa", new Object(), new ArrayList(), {} };

But not here:

Object[] obj = new Object[]{ "aaa", new Object(), new ArrayList(), new Object[]{} };

If I am not wrong, both {} and new Object[] are arrays, which in turn are objects. What I am missing here?

StatelessDev
  • 294
  • 4
  • 15
  • 2
    I'm out of votes, but here's the relevant duplicate: [Array initialization syntax when not in a declaration](http://stackoverflow.com/questions/5387643/array-initialization-syntax-when-not-in-a-declaration) or [this](http://stackoverflow.com/questions/17515096/string-array-initialization-in-java?lq=1). It's not allowed by the syntax. – Sotirios Delimanolis May 25 '16 at 19:35
  • You can use `SomeType[] blub = {...}` because the type of the array is clear, but what is the type of `new Object[]{ ..., {} }`? Is the inner one an `int[]`, a `String[]`, a `Blub[]` or something different? How should the compiler know that? The array initializer `{}` is very strict (like explained here: [Array initialization syntax when not in a declaration](http://stackoverflow.com/q/5387643)), so don't expect much convenience. – Tom May 25 '16 at 19:38

0 Answers0