1

I know that an object is a class instance or an array.

Meaning that when creating an array, an instance of a real class is not created (the classes are created dynamically and are not defined in the source code).

According to the JLS https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.8

Every array has an associated Class object, shared with all other arrays with the same component type.

Although an array type is not a class, the Class object of every array acts as if:

  • The direct superclass of every array type is Object.

  • Every array type implements the interfaces Cloneable and java.io.Serializable.

Is the Class object associated with the dynamically created class?

Why does the Class object allow arrays to inherit java.lang.Object?

EDIT:

To clarify my question.

According to https://stackoverflow.com/a/2267807/11306652

"arrays are Objects in the sense that they inherit java.lang.Object, the classes are created dynamically as a special feature of the language. They are not defined in source code."

Furthermore,

https://stackoverflow.com/a/10674614/11306652

"[I" is the name of the class which we would call, in English, "array of int". The class is a "full citizenship" Java class, which responds to all the methods of Object. The only difference is that the new syntax is different and it doesn't support the newInstance() method of Class.

The classes "[I", "[C", et al, are "pre-defined" in the JVM -- there are no .class files corresponding to them. Java also will implicitly create, on the fly,"

Is my understanding correct that when arrays are created, the JVM dynamically creates an "array class" (with Object as a superclass) that defines the array object? Additionally there is a Class object associated with the dynamically created "array class".

Community
  • 1
  • 1
csguy
  • 1,354
  • 2
  • 17
  • 37
  • *an instance of a real class is not created (the classes are created dynamically and are not defined in the source code)* This seems confused. The truth is simply that an array of something, like int, boolean, float, etc., is not an object of any class. There are no "dynamically created classes" involved. Nevertheless, it is convenient for there to be a Class object ("Class", not "class") because Class objects hold useful information about a type. That the Class says the array inherits from Object does not make it true. –  May 09 '19 at 02:31
  • @another-dave i was unaware that was incorrect, i got that info from https://stackoverflow.com/questions/2267790/how-are-arrays-implemented-in-java#comment47279973_2267790 see top answer. – csguy May 09 '19 at 02:34
  • That answer is not appropriate. Consider "There is no such source code for that. You have created it in code dynamically". What is the declaration for ```MySpecialObject[] array``` if not source code? I suspect the writer is confusing "the class of the array" (there isn't one, an array is not an object of any class) and "the Class object for the array type" (the [class Class](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html) holds data about the type T). –  May 09 '19 at 02:40
  • 1
    The short answer: *"Why does the Class object allow arrays to inherit java.lang.Object??"* - Because, actually, in the context of the complete Java language design ... it makes sense. – Stephen C May 09 '19 at 02:40
  • 1
    The 2nd short answer: *"Is the Class object associated with the dynamically created class?"* - You can get a `Class` object for every object, including objects whose class was dynamically created. (And arrays ... because arrays are objects.) – Stephen C May 09 '19 at 02:43
  • @StephenC another-dave states that no dynamically created classes are involved? – csguy May 09 '19 at 02:48
  • That is mostly true. Array types are *typically* not created dynamically, but they could be if you create array objects via reflection. However, my previous short answer is correct **irrespective** of that. (That's why I said "short answer"!) – Stephen C May 09 '19 at 02:53
  • @StephenC can you elaborate on how array types are not typically created dynamically, according to "The classes "[I", "[C", et al, are "pre-defined" in the JVM -- there are no .class files corresponding to them. Java also will implicitly create, on the fly" [link](https://stackoverflow.com/a/10674614/11306652) – csguy May 09 '19 at 02:59
  • Edit your question, and I / we will consider whether it is sufficiently distinct from the dup to be reopened. – Stephen C May 09 '19 at 03:07
  • But also, bear in the mind that some people who answer Questions are inclined to be a bit "loose" with their use of the terminology. – Stephen C May 09 '19 at 03:10
  • @StephenC i tried my best to edit – csguy May 09 '19 at 03:18
  • 1
    1. Yes. Both the JLS and the JVM Spec state clearly that a class object for an array is dynamically created. [JLS #10.8 'Class Objects for Arrays'](http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.8) and [JVM Spec #5.3.3 'Creating Array Classes'](https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-5.html#jvms-5.3.3). And JLS #10.8 states clearly that 'the direct superclass of every array type is Object.' 2. You can get the `Class` object via the `.getClass()` method on the array instance, as for any other object. – user207421 May 09 '19 at 03:25
  • @user207321 i understand that a Class object is dynamically created, but i was wondering if there was a dynamically created "array class" with a dynamically created Class object – csguy May 09 '19 at 03:30
  • @user207421 so the dynamically created Class object defines the array object? sorry im a student, im trying my best but i have no idea whats going on – csguy May 09 '19 at 03:32
  • No, `int[] array = new int[10]` defines the object; `array.getClass()` gives you the `Class`. – user207421 May 09 '19 at 03:34
  • @user207421 which returns "[I" which is what? the array's class that is dynamically created by jvm? – csguy May 09 '19 at 03:36
  • That is the name of the dynamically created array class, as given by `array.getClass().getName()` or `array.getClass().toString()` in the above. [If you `print()` the class it automatically calls `toString()`.] – user207421 May 09 '19 at 03:38
  • @user207421 oh thats what ive been wondering the whole time.. when arrays are created, the JVM dynamically creates an "array class" (with Object as a superclass) that defines the array object? Additionally there is a Class object associated with the dynamically created "array class". – csguy May 09 '19 at 03:39
  • 1
    That is correct. NB The answer that was described above as 'not appropriate' is indeed appropriate, and the reason advanced above is nonsense, as is 'an array is not an object of any class'. – user207421 May 09 '19 at 03:40
  • 2
    @another-dave Please don't post BS here. It just causes pointless confusion. Everything you've stated here is directly contradicted by the JLS and JVM Spec. – user207421 May 09 '19 at 03:51
  • There are statements in the JLS and the JVM spec saying that an array type is not a class type. –  May 09 '19 at 11:25
  • JLS 8, section 10.8. JVM 8, section 2.4. I grant you that wording in JVM section 5.3.3 contradicts what is says in 2.4. –  May 09 '19 at 11:38

0 Answers0