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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:42:14+00:00 2026-06-10T09:42:14+00:00

Possible Duplicate: Create instance of generic type in Java? Java how to: Generic Array

  • 0

Possible Duplicate:
Create instance of generic type in Java?
Java how to: Generic Array creation

I am trying to create a create a class of generic type. This is my class file.

public class TestClass<T> implements AbstractDataType<T>{

    T[] contents;

    public TestClass(int length) {
        this.contents = (T[])new Object[length];
    }
}

But the contents have just have the methods inherited from the Object class. How can I create an abstract array for contents ?

  • 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-10T09:42:16+00:00Added an answer on June 10, 2026 at 9:42 am

    As far as initializing contents, I think what you have is the best you can do. If there way a way, ArrayList would probably do it (line 132: http://www.docjar.com/html/api/java/util/ArrayList.java.html)

    But when you say “the contents have just have the methods inherited from the Object class”, I’m assuming you mean that you can only access methods like toString and equals when you are working with a T instance in your code, and I’m guessing this is the primary problem. That’s because you’re not telling the compiler anything about what a T instance is. If you want to access methods from a particular interface or type, you need to put a type constraint on T.

    Here’s an example:

    interface Foo {
      int getSomething();
      void setSomethingElse(String somethingElse);
    }
    
    public class TestClass<T extends Foo> implements AbstractDataType<T> {
      T[] contents;
    
      public TestClass(int length) {
        this.contents = (T[])new Object[length];
      }
    
      public void doSomethingInteresting(int index, String str) {
        T obj = contents[index];
        System.out.println(obj.getSomething());
        obj.setSomethingElse(str);
      }
    }
    

    So now you can access methods other than those inherited from Object.

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

Sidebar

Related Questions

Possible Duplicate: Create instance of “Class” Using “Reflection” in JavaScript Java has Class.forName(XYZClass) to
Possible Duplicate: Abstract class constructor in Java When we can't create an instance of
Possible Duplicate: Create Excel file in Java How to save output in excel format
Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of
Possible Duplicate: Is there a call_user_func() equivalent to create a new class instance? I
Possible Duplicate: How do I use define_method to create class methods? I am trying
Possible Duplicate: Reflection - Getting the generic parameters from a System.Type instance Get actual
Possible Duplicate: Why is (javax.servlet.)SingleThreadModel deprecated? A servlet container generally create one instance of
Possible Duplicate: Create an Array of the Last 30 Days Using PHP I am
Possible Duplicate: PHP create a file without fopen I want to create a file

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.