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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:37:06+00:00 2026-05-26T19:37:06+00:00

I am studying generics now and I wonder how to build a parametrized class

  • 0

I am studying generics now and I wonder how to build a parametrized class that can store some data for me. I want to know because of c++ influence on me. Secondly, I wonder how ArrayList is built.

My problem is you cannot make new instance of a class that has no default constructor, i.e. Integer. I know I should specialize my class in order to handle these classes individually. However, there is no specialization in Java as in c++.

How can I alter the code

import java.lang.*;

class test {}

class generic<T> {
    private Class<T> type;
    T element;
    generic(Class<T> type) {
    this.type = type;
    try {
        element = type.newInstance();
    } catch (Exception e) {
        System.out.println("error " + e);
    }
    }
    static <T> generic<T> create(Class<T> type) {
    return new generic<T>(type);
    }
}

public class Generic {
    public static void main(String[] args) {
    generic<Test> test = generic.create(Test.class);
    System.out.println(test);
    }
}

so that it handles Integer instead of Test?

  • 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-26T19:37:06+00:00Added an answer on May 26, 2026 at 7:37 pm

    My reaction to your class is it attempting to do two things. Create the data and store the data. As you have discovered with your experience with the Integer class, there are many classes in Java that do not expose a public constructor let alone the default constructor. So there is no single way to create object instances that can be relied on for all possible types you will encounter. You state that your requirement is to store data so let’s remove the responsibility of creating data:

    class Test {
        public String toString() {
            return "test";
        }
    }
    
    class Generic<T> {
        private T element;
    
        Generic(T element) {
            this.element = element;
        }
    
        public String toString() {
            return element.toString();
        }
    }
    
    public class GenericTest {
        public static void main(String[] args) {
            Generic<Test> testGeneric = new Generic<Test>(new Test());
            System.out.println(testGeneric);
    
            Generic<Integer> integerGeneric = new Generic<Integer>(3);
            System.out.println(integerGeneric);
        }
    }
    

    As for wondering how ArrayList is built, Java is open source so you’re free to inspect how all the SDK types are implemented.

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

Sidebar

Related Questions

Possible Duplicate: Instantiating a generic class in Java I am studying Generics Java because
while studying some security things, there was a question that one can guess the
While studying the Collection API, we find that some methods ( add , remove
Currently studying bitwise arithmetic. It's really easy, because I have some CS background. But
I studying NSCharacterset class. NSString and NSCharacterset look like similar class. Who can explain
I've been studying code of some different libraries, and notice that some will provide
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's
After studying TCP/UDP difference all week, I just can't decide which to use. I
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
I am studying Data Structures in java and I am having difficulty with using

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.