Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:14:38+00:00 2026-05-23T12:14:38+00:00

In the first code example of this beginners guide to Dependency Injection I encountered

  • 0

In the first code example of this beginners guide to Dependency Injection I encountered some new constructs of which I am not sure that I totally 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 new Class[]{AirlineAgency.class} actually mean and do?

I understand that its goal is to create a Constructor instance for AirlineAgency.class but how does the syntax new Class[]{} achieve this?

Why the array notion [] when there is only one object involved?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:14:39+00:00Added an answer on May 23, 2026 at 12:14 pm

    new Class[] { AirlineAgency.Class } creates an one-element array of Class objects and initializes the only element to be AirlineAgency.class. It is similar to new int[] { 42 }.

    The code is essentially equivalent to this:

    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);
    

    The Class.getConstructor method wants an array of parameter types for the constructor (to find the right overload to use), and similarly Constructor.newInstance wants an array of arguments. That’s why it’s done that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of classes (for this example anyway) that use code first
For example, will the first piece of code perform a full search twice, or
First, an example of something that works as expected: (all code was executed in
Which one below is correct? First code has no quotes in the $_GET array
Code first: '''this is main structure of my program''' from twisted.web import http from
I've been started studying PHP in my spare time, and the first code example
This is my first posting on SO, and I'm relatively new to Haskell, so
I'm taking my first crack at writing some linux kernel code, and I'm hitting
I have this (example) code: init() class A: foo = bar() def __init__(self): print(A.foo)
This is the second step of a question explained here: EF 4.1 code-first: How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.