0

Possible Duplicate:
Create instance of generic type in Java?

In C# I can use the class Activator or default(T) but how can i do that in Java? I have a custom class like

public class Foo<T extends FooBase> ... 

Now i want to create a instance of T(it has a public default ctor). I am new in java and I have no idea how I could create a instance of that "type". I've tried something like this:

T.class.newInstance();

but T has no .class? So what can I do?

Community
  • 1
  • 1
Florian
  • 5,918
  • 3
  • 47
  • 86
  • 1
    what's wrong with calling the constructor like `new Foo()` (where `class Bar extends FooBase`) ? – Nevik Rehnel Jan 03 '13 at 14:49
  • 1
    http://stackoverflow.com/a/12407106/1193090 answers your question nicely! – Reigo Jan 03 '13 at 14:52
  • Looking at your question again, i think you might have a wrong concept of how generics work in java. you should read up on it, e.g. the answer and tutorial linked by Reigo above – Nevik Rehnel Jan 03 '13 at 14:53
  • No, you can't: http://stackoverflow.com/questions/6101568/java-how-to-set-a-default-for-t-in-someclasst – Andrea Ligios Jan 03 '13 at 14:57

2 Answers2

-1

You can use only existing constructor

public class Foo<T extends FooBase>{
    T base= (T) new FooBase();
}
Gangnus
  • 24,044
  • 16
  • 90
  • 149
-4

Class your_class = new Class() ;

pidel
  • 65
  • 9