• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

question from generics.  RSS feed

 
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am wondering if this will work or not

public class TwoGenericArgument<X super Integer,Y>
{
}

I think here the super keyword will accept anything that is either the type T or super of T. But when I pass an Integer to X,it fails.
Any explanations?
 
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thing because it is a class not a method.

suppose that it compiles.

public class TwoGenericArgument<X super Integer,Y> { //compil time error
X a=new X();
void foo(){
a.intValue();
}
}


when you pass an object of Object class and you will call
TwoGenericArgument<Object,String> n=new TwoGenericArgument<Object,String>();
n.foo();

now you are in foo method but as you will check a is a reference of Object class but Object does not have any intValue() method which is a compiler error. This error can't be detected through this way.

One more thing is here when you are instantiating it compiler does not know about which constructor you wants to call.

it only knows about the Integer class.
so,its instantiation must be
X x=new X(4);

and now when you pass object of Object class then Object class does not have this type of constructor.
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i passed the value of Integer via the constructor. ok see this code:
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!