how can we Create instance for generic type in java

0 votes

Is it possible to create an instance of a generic type in Java? I think it is not possible still want to know if it is possible

class BetaContainer<E>
{
    E createContents()
    {
        return what???
    }
}
Jun 8, 2018 in Java by sharth
• 3,370 points
26,855 views

2 answers to this question.

0 votes

You can with a classloader and the class name, eventually with some parameters shown in the code below

final ClassLoader classL = ...
final Class<?> aClass = classL.loadClass("java.lang.Integer");
final Constructor<?> constr= aClass.getConstructor(int.class);
final Object obj = constr.newInstance(123);
System.out.println("o = " + obj);

Hope it helps!

If you need to know more about Java, join our Java online course today.

Thanks

answered Jun 8, 2018 by Daisy
• 8,140 points
0 votes
private static class SomeContainer<E> {
    E createContents(Class<E> clazz) {
        return clazz.newInstance();
    }
}
answered Aug 27, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
2,960 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
950 views
0 votes
1 answer

How can we get file extension in Java?

To get the file extension, we can ...READ MORE

answered May 8, 2018 in Java by Parth
• 4,640 points
3,022 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method ...READ MORE

answered May 24, 2018 in Java by UshaK
2,768 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 19, 2018 in Java by sharth
• 3,370 points
802 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,095 views
0 votes
2 answers

How do i create generic array in Java

as first tip you cannot assign value ...READ MORE

answered Feb 13, 2020 in Java by sama
17,245 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,703 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,770 points
117,991 views
0 votes
4 answers

How can we compare dates in java?

public static String daysBetween(String day1, String day2) ...READ MORE

answered Sep 5, 2018 in Java by Sushmita
• 6,920 points
1,426 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP