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

  • Home
  • SEARCH
  • 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 735273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:28:12+00:00 2026-05-14T07:28:12+00:00

I have come across a class that has an immutable property: MyObject[] allObjs The

  • 0

I have come across a class that has an immutable property:

MyObject[] allObjs

The property is initialized like this:

List<MyObject> objs = createAllMyObjects();
allObjs = objs.toArray(new MyObject[objs.size()]);

When it is exposed through the accessor, it’s done as a List:

public List<MyObject> getAllMyObjects() {
    return Collections.unmodifiableList(Arrays.asList(allObjs));
}

Why would a programmer do this? Is there a benefit that I don’t know about?

Performance is not a concern, as the array of objs will only ever number in a few dozen elements.

It seems that we are going round and round in circles.

The class is a sort of factory, so it’s got a private constructor and exposes only static methods (not sure if this could be a reason for the madness).

edit

I guess that my question is really, “Why not just use an internal List<MyObject> allObjs instead of MyObject[] allObjs, and return the Collections.unmodifiableList(allObjs)?” as this would accomplish the same thing.

  • 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-14T07:28:13+00:00Added an answer on May 14, 2026 at 7:28 am

    The only reason I see is “fail early in case of misuse” (if this design choice was not random and has a meaning, the meaning is he trusts very little his particular clients).

    MyObject[] is reified, and checked at runtime, while List<MyObject> is not. One can sneak non-MyObject references in the latter (he’ll get unchecked warnings at compilation of course), which will fail at some undefined future point with a ClassCastException, but he cannot do the same with MyObject[] – that will fail immediately at the point of misuse.

    Note though that if the clients are prebuilt binaries that was using this API before it was generified, then nobody got unchecked warnings, so if this code is trying to migrate code used by pre-generics clients, it makes sense.

    (Another way to achieve the same result would be using List<T> and also requiring Class<T>, so that the latter can offer the runtime type checks. But that would require for example another constructor parameter – existing clients wouldn’t automatically take advantage of that. **Update: ** This basically describes the API call mentioned in the first comment by Sauer, so one does not have to reinvent this, just use that one 🙂 ).

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

Sidebar

Related Questions

No related questions found

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.