-6

Please answer to my question.

class parent<T>
{
 T ob;
 public parent();
 ob=new T(); //Giving error for this statement ,Parameterized can not be created
 }
alakhya
  • 35
  • 1
  • 7
  • 3
    That's a really strange way to ask question. Perhaps you should refer to http://stackoverflow.com/help/how-to-ask – Balwinder Singh Aug 11 '16 at 05:23
  • 1
    What is your question? – Robert Columbia Aug 11 '16 at 05:24
  • 1
    Refer http://stackoverflow.com/questions/313584/what-is-the-concept-of-erasure-in-generics-in-java – Unknown Aug 11 '16 at 05:27
  • My Question is that,Eraser works like, Java eraser removes all the parameter type'T' with their bounded type,(If no bounded type specfied) it will replace the 'T' with Object. ob=new T(); (it will be ob=new Object();) why complier throws error. – alakhya Aug 11 '16 at 05:29
  • 2
    You asked this same question two months ago (http://stackoverflow.com/questions/37591889/generic-erasure-concept) and accepted an answer. What don't you understand? If you didn't understand, why did you accept an answer? – Robert Columbia Aug 11 '16 at 05:30
  • T might be a value type or might not have a default constructor. – Robert Columbia Aug 11 '16 at 05:33

1 Answers1

0

You cannot instantiate type parameters. You might want to read Oracle - Restrictions on Generics

Andreas Brunnet
  • 722
  • 6
  • 19
  • Is it exception to the oracle eraser concept ?? – alakhya Aug 11 '16 at 05:32
  • No, it's the rule. – Robert Columbia Aug 11 '16 at 05:33
  • @alakhya Yes it cannot be done because of type erasure. – Andreas Brunnet Aug 11 '16 at 05:37
  • Just curious to know the reason behind it – alakhya Aug 11 '16 at 05:40
  • 1
    It's because the compiler does not have any knowledge about the type. Therefore it does not know if there is a default constructor available or not. – Andreas Brunnet Aug 11 '16 at 05:44
  • @AndreasBrunnet ..Compiler replace 'T' with "Object" ,Object has constructor. – alakhya Aug 11 '16 at 06:11
  • @alakhya Thats true. But its at compile time. At runtime it actually does not know which type it is currently handling. It might be a class without a default constructor or an interface which has been represented by the type. So it does not know if it is possible to create a new instance of the unknown type. – Andreas Brunnet Aug 11 '16 at 06:20
  • @AndreasBrunnet before compile code is Ob=new T(); After compile code will be Ob=new Object(); at run it is present ? Please make me correct here if i am wrong – alakhya Aug 11 '16 at 16:02
  • @alakhya It's correct that there is an Object. Assume you use your generic code with a class that does not have a default constructor. What would happen? Java tries casting the Object into your MyFancyObject. The result -> ClassCastException at runtime. So Java was designed (the compiler) to prevent such a runtime error and instead tell you, that this code won't even be accepted (because else there could be errors at runtime). It's a way of preventing unessessary errors. – Andreas Brunnet Aug 12 '16 at 04:26