0

I found an instantiation wich I do not understand how works. The instantiation looks like this:

public static Class instance[] = new Class[arraySize];

If my guessing is right, the instance is an array? How will this work?

  • 1
    It creates an array of Class references ("pointers" to Class objects), with `arraySize` number of elements. – Hot Licks Jul 07 '14 at 00:00

2 Answers2

1

This declares an array of Class objects references. It is equivalent to the other syntax of [] after the type.

You would access it just like a normal array:

 instance[0] = ...
 instance[1] = ...
Community
  • 1
  • 1
merlin2011
  • 63,368
  • 37
  • 161
  • 279
1
public static Class instance[] = new Class[arraySize];

public is the access modifier. This one means that this variable is visible in your entire project static means that this variable is a "class" field it means that it belongs to the entire class and you can access it by ClassName.nameOfTheVariable or if you are accessing it from inside of class it is declared you could use just nameOfTheVariable.

Class in this context is a type and you should treat is as a type of object

[] means that this is an array you could also write Class[]

= is assignment operator

new is the word that annunciates that there will be memory allocation and constructor invokation after it

After new there is initialization of array of arraySize length.

Yoda
  • 15,011
  • 59
  • 173
  • 291
  • Good point: Java allows the declarations `ClassName[] instanceName` and `ClassName instanceName[]` to be used interchangeably -- both mean an array of object references to `ClassName` objects. – Hot Licks Jul 07 '14 at 00:09
  • @Yoda ..Nice answer. I wonder why this answer was never upvoted!! – N Czar Jan 28 '17 at 03:16