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 5957637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:25:45+00:00 2026-05-22T18:25:45+00:00

Sorry for the cryptic title, but this is difficult to explain. The general rule

  • 0

Sorry for the cryptic title, but this is difficult to explain. The general rule is that I need a lazy loader that will give me N instances of a bound wildcard type. I’m calling the lazy loader a storage unit.

import java.util.ArrayList;
import java.util.List;

public class StorageUnit<T extends MyInterface> implements Storable<T> {

    private int count;

    public StorageUnit(int count) {
        this.count = count;
    }

    private List<T> storables = new ArrayList<T>();

    public List<T> getInstances(Class<T> c) {
        try {
            if (storables.size() == 0) {
                for (int i = 0; i < count; i++) {
                    storables.add(c.newInstance());
                }
            } else {
                return storables;
            }
        }catch (IllegalAccessException illegalAccessException) {
            illegalAccessException.printStackTrace();
        } catch (InstantiationException instantiationException) {
            instantiationException.printStackTrace();
        }

        return storables;
    }
}

Elsewhere in my application I have another class that has a reference to several of these storage units. I need to get instances of the storage unit type, and then I will do something with that type.

import java.util.ArrayList;
import java.util.List;

public class MyStorageUnitContainer {

    private List<StorageUnit<? extends MyInterface>> storageUnits;

    public MyStorageUnitContainer(List<StorageUnit<? extends MyInterface>> storageUnits) {
        this.storageUnits = storageUnits;
    }

    public List<StorageUnit<? extends MyInterface>> getInstances() {
        List<StorageUnit<? extends MyInterface>> instances = new ArrayList<StorageUnit<? extends MyInterface>>();
        for (StorageUnit<? extends MyInterface> storageUnit : storageUnits) {

            storageUnit.getInstances(/* I can't get the bound wildcard... */);
            // Now loop through those instances and do something with them...

        }
        return instances;
    }
}

That code sucks, so the best analogy I can think of is an actual storage unit container. That storage unit container has several individual storage units (think boxes). Each one of those boxes contains items of a certain type (think baseball cards). I know that a box contains 100 baseball cards, but until I open the box I don’t know anything about the details of each baseball card. I’m basically trying to treat each box as a lazy loader. Opening the box loads N implementations if they don’t exist already.

  • 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-22T18:25:46+00:00Added an answer on May 22, 2026 at 6:25 pm

    Paulo is correct, and (also as per Paulo’s answer), I often just pass a class object into a constructor to get around this problem. It allows the getInstances() method to appear as you would like it – ie without parameters. Internally, the instance keeps a reference to the generic class so it can call newInstance() on it.

    This code illustrates this using your example. I have tested this an it executes OK.

    import java.util.ArrayList;
    import java.util.List;
    
    public class Sandbox
    {
        static interface MyInterface
        {
        }
    
        static interface Storable<T>
        {
            List<T> getInstances();
        };
    
        static abstract class MyStorableImpl implements MyInterface
        {
            @Override
            public String toString()
            {
                return "I'm a " + getClass() + " with hashcode " + hashCode();
            }
        }
    
        static class MyStorable1 extends MyStorableImpl
        {
        }
    
        static class MyStorable2 extends MyStorableImpl
        {
        }
    
        static class StorageUnit<T extends MyInterface> implements Storable<T>
        {
            private final int count;
    
            private final Class<T> clazz;
    
            public StorageUnit(Class<T> clazz, int count)
            {
                this.count = count;
                this.clazz = clazz;
            }
    
            private List<T> storables = new ArrayList<T>();
    
            public List<T> getInstances()
            {
                try
                {
                    if (storables.size() == 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            storables.add(clazz.newInstance());
                        }
                    }
                    else
                    {
                        return storables;
                    }
                }
                catch (IllegalAccessException illegalAccessException)
                {
                    illegalAccessException.printStackTrace();
                }
                catch (InstantiationException instantiationException)
                {
                    instantiationException.printStackTrace();
                }
    
                return storables;
            }
        }
    
        static class MyStorageUnitContainer
        {
    
            private List<StorageUnit<? extends MyInterface>> storageUnits;
    
            public MyStorageUnitContainer(List<StorageUnit<? extends MyInterface>> storageUnits)
            {
                this.storageUnits = storageUnits;
            }
    
            public List<MyInterface> getAllInstances()
            {
                List<MyInterface> instances = new ArrayList<MyInterface>();
                for (StorageUnit<? extends MyInterface> storageUnit : storageUnits)
                {
                    List<? extends MyInterface> list = storageUnit.getInstances();
                    instances.addAll(list);
                }
                return instances;
            }
        }
    
        public static void main(String[] args)
        {
            StorageUnit<? extends MyInterface> box1 = new StorageUnit<MyStorable1>(MyStorable1.class, 2);
            StorageUnit<? extends MyInterface> box2 = new StorageUnit<MyStorable2>(MyStorable2.class, 3);
            List<StorageUnit<? extends MyInterface>> boxes = new ArrayList<Sandbox.StorageUnit<? extends MyInterface>>();
            boxes.add(box1);
            boxes.add(box2);
    
            MyStorageUnitContainer container = new MyStorageUnitContainer(boxes);
            List<MyInterface> allInstances = container.getAllInstances();
            for (MyInterface myInterface : allInstances)
            {
                System.out.println(myInterface.toString());
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this sounds like a really stupid question, but I need to make
Sorry for the vague title but I really didn't know what title to give
Sorry that this may seem like a rookie question, but it's a real pain
I am sorry for the cryptic title but I didn't know how to adequately
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry for long winded title, this makes a lot more sense with an example.
Sorry for the lengthy introduction but I think it best to explain the context
Sorry if this question is mind numbingly easy to answer, but I'm a bit

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.