How does the syntax of `new Class [] {}` work?

In the first code example of this guide for newcomers to Injection Dependency, I came across some new constructs that I'm not sure about that I fully understand:

 // Instantiate CabAgency, and satisfy its dependency on an airlineagency.

 Constructor constructor = cabAgencyClass.getConstructor
  (new Class[]{AirlineAgency.class});
 cabAgency = (CabAgency) constructor.newInstance
  (new Object[]{airlineAgency});

What does it mean new Class[]{AirlineAgency.class}and does?

I understand that his goal is to create an instance Constructorfor AirlineAgency.class, but how does the syntax do this new Class[]{}?

Why is the concept of an array []when there is only one object?

What is the syntax {}here? Why not ()?

+5
source share
1 answer

new Class[] { AirlineAgency.Class } Class AirlineAgency.class. new int[] { 42 }.

:

Class[] parameterTypes = new Class[1];
parameterTypes[0] = AirlineAgency.class;

Constructor constructor = cabAgencyClass.getConstructor(parameterTypes);

Object[] arguments = new Object[1];
arguments[0] = airlineAgency;

cabAgency = (CabAgency)constructor.newInstance(arguments);

Class.getConstructor ( ) Constructor.newInstance . .

+6

All Articles