in C#, we can define a generic class A<T> where T : new()
. In this code, we can create an instance of T
with new T()
. How does this implement in Java? I read some article which says it's impossible.
The reason that I used have a singleton patten using generic in C# like:
public static class Singleton<T> where T : new()
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = SingletonCreater.Instance;
}
return instance;
}
}
static class SingletonCreater
{
internal static readonly T Instance = new T();
}
}
And way to make this method more graceful?