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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:41:12+00:00 2026-06-11T02:41:12+00:00

I have a class FirstClass<O> and a class SecondClass<O> , and I want to

  • 0

I have a class FirstClass<O> and a class SecondClass<O>, and I want to make an O[] in SecondClass<O> inside a routine which is called from FirstClass<O>, where O is a generic class parameter. I fail to find how to do this.

I need an O[] specifically (and not ArrayList<O> or similar) because I need to get elements from it very often inside the body of a loop, and it matters for the execution time of my algorithm.

So I would want something along these lines.

public class FirstClass<O> {
    void someRoutine(n and other params) {
        //Do some stuff
        SecondClass<O> = new SecondClass(n, possibly_other_params);
        //Do some stuff
    }
}

and

public class SecondClass<O> {
    O[] myArray;
    SecondClass<O>(int n, possibly_other_params) {
        //Here the constructor that creates an O[n]
    }
}

Some methods I found on the web, but do not work for my case:

  • Use O[] array = (O[]) new Object[n]; but that doesn’t compile.
  • Use Object[] array = new Object[n]; and do an (O) cast every time I request something from the array, but this is way too slow
  • Use Array.newInstance(Class<O> type, int n);, with O o; and type=o.class but it complains that type is now of type Class<CAP#1> instead of type Class<O>, whatever CAP#1 means…

How should I do this properly in Java, with optimal execution speed in mind?

  • 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-06-11T02:41:14+00:00Added an answer on June 11, 2026 at 2:41 am

    Java’s handling of generics is in pretty rough shape but you can accomplish something close to what you want if you are prepared to make small adjustments. Please see:

    public class FirstClass<O> {
        Class<O> type;
    
        public static <O> FirstClass<O> create(Class<O> type) {
            return new FirstClass<O>(type);
        }
    
        public FirstClass(Class<O> type) {
            this.type = type;
        }
    
        public void routine(int size /*, other params */ ) {
            SecondClass<O> instance = new SecondClass<O>(type, size);
        }
    }
    
    public class SecondClass<O> {
        public O[] array;
    
        @SuppressWarnings("unchecked")
        public SecondClass(Class<O> type,int size) {
            array = (O[])Array.newInstance(type,size);
        }
    }
    

    And a use case:

    FirstClass<Integer> instance = FirstClass.create(Integer.class);
    instance.routine(110);
    

    This is just a rough example although I am sure you could accomplish something similar without having to use this approach.

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

Sidebar

Related Questions

I have this middleware which is the first: class RedirectIt require net/https require uri
I have a web application and two class files, First class is MyClass.class which
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
In Visual Studio 2008 I have a Class Library project (called Media ) to
Let's say I have two classes. Each class has one parameter. Parameter of first
Which static class initializes first if we have one more static classes in our
If I have a class which has an interface definition like below; public class
I need to have two classes, one class has two methods each of which
I have 2 classes that inherits from a father class. The father class has

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.