Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Instantiating object from generic type: Java vs. .Net

2 min read

Consider the following .Net code:
public static class Processor
{
public static TItem CreateInstance<TItem>() where TItem:new()
{
TItem result = new TItem();
return result;
}
}
This is a pretty straightforward class, although not so meaningless if left in this way. You may use this piece of code as follows:
class Program
{
static void Main(string[] args)
{
Test t = Processor.CreateInstance<Test>();
Console.Write(t.SomeInt);
}
}

class Test
{
public int SomeInt { get; set; }

public Test()
{
SomeInt = 3;
}

}
Nothing strange so far. What I want to show however is how simple it is to create an instance of a generic type in C#.

Now let's switch over to the Java world where I'd like to realize a similar thing. So first, create a class Processor again with a static method createInstance.
public class Processor {
public static <T> T createInstance(){
T t = new T();
return t;
}
}
Probably that's what you would write or better, would like to write since this will give you a nice compiler error. You cannot do "new T()" where T is a generic type parameter. So now the challenge starts. How can I create an instance. It turns out that you need the class for doing so s.t. it looks like
public class Processor {
public static <T> T createInstance(Class clazz) throws InstantiationException, IllegalAccessException{
T t = (T) clazz.newInstance();
return t;
}
}
And then you invoke it like
public class MainClass {

public static void main(String[] args) throws InstantiationException, IllegalAccessException {
Test t = Processor.createInstance(Test.class);
System.out.println(t.getSomeInt());
}
}
Note in Java you don't have to pass the generic type. The compiler will understand it outmatically. That's nice. However when looking at the overall solution, the C# example seems much more elegant. If you look at the "createInstance" method in the Java example you may also note that providing the class is somehow a duplicate information. If you're a clean-code fanatic like myself the immediate thought would be to somehow get this information form the generic parameter type directly. Through reflection? It turns out that to be a major challenge. Java has a mechanism called type erasure, which eliminates generics information at runtime in order to stay backward compatible. Read this article for more information.
Another guy which seemed to have the same problem proposed a possible workaround for this: happy reading ;)

Usually then, passing the class information in some acceptable way is the simpler and probably type-safer solution.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus