2

I'm not a Java programmer but have to do some Java work. From some googling, this seems like an anonymous subclass. But then the Object is followed by "[]". But then again the variable is declared is an Object. Is this just contravariance with the most generic type (Object) allowing an Array of Objects?

Object thing = new Object[] {someInt, anotherInt, someInterface, someString};
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
lanza
  • 1,512
  • 2
  • 13
  • 26
  • 4
    as a sidenode, using an `Object[]` to store different types isn´t the best idea to smoothly work with. – SomeJavaGuy Jun 10 '16 at 06:46
  • Yup, not my code. Reading somebody else. Though I guess in this case it's a onetime thing to pass these for values but still a bit hacky. – lanza Jun 10 '16 at 06:50
  • @lanza I'd consider that a warning sign. As Kevin said, using an `Object[]` isn't very smooth (it's clearly a class of some sort), and the assignment to `Object` just adds to the What The Fudge factor. – Kayaman Jun 10 '16 at 06:52

1 Answers1

6

No. Arrays are final so you can't subclass them. The code just creates an object array Object[] and at creation time fills it with some ints (autoboxed to Integer), an interface and a String.

The "odd" thing about the example is that it assigns the Object[] to an Object reference. Now all classes extend Object including arrays, so this is valid, but I can't imagine any case where it would make sense to have Object thing instead of Object[] thing unless you're intentionally trying to make your code less clear.

Kayaman
  • 72,141
  • 5
  • 83
  • 121