Can java variables be an unbounded wildcard? -


for example, can variable so:

public <? extends foo> fooextension = new fooaugmentation(); 

without throwing errors, or there not implementation of this?
example of how useful so:

public class human{     private string localname;     private <? extends animal> favoriteanimalinstance;     public human(string name, <? extends animal> fav){         localname=name;         favoriteanimalinstance = fav;     }     public <? extends animal> getfavoriteanimal(){     return favoriteanimalinstance();     } } 

with main code:

public class main{     public static void main(string[] a){         human animatedcharacter = new human('fry',new seymour());         system.out.println(animatedcharacter.getfavoriteanimal().ispetrifiedindiamondium()?          "not episode dog" :          "it's episode dog..");     } } 

or on complicating done more that?

since discussion starts out of hand...

your code, if valid, result in human object had favorite animal. human have no way of telling compiler, specific type of animal was, since knew "something extends animal (or animal itself)".

effectively, same as...

private animal animal; 

obviously, don't want want hard-code animal there...

private seymore favoriteanimalinstance; 

so, real "choice" have, making instance of human know favorite animal is...

public class human<a extends animal> {     private favoriteanimal;     public getfavoriteanimal(){       return favoriteanimal;    } }  ... public static void main(string[] args) {     human<seymour> fry = new human<seymour>();    // etc.    boolean petrified = fry.getfavoriteanimal().ispetrifiedindiamondium(); } 

besides that? not many choices, since compiler needs know type of object before can call methods on it. if compiler knows "animal", have animal methods, if actual object during runtime extremly more complex , has dozens of other methods.

and yes, reflection analyze object @ runtime , call methods, etc. - trust me, that's not idea , doesn't solve problem.

oh, , please start variable names lower case, thanks. "instance" pretty superfluous here, since not class, instance, doesn't add meaningfull here.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -