java - Type declaration based on type parameters in inner classes -


does java shadow type parameters? finding hard test myself because java generics not reified @ run time.

for example, given code:

public class nestedgeneric<t> {     private innergeneric<t> innergenericinstance;      private static class innergeneric<t> {         public t innergenericfield;     }      nestedgeneric() {     innergenericinstance = new innergeneric<t>();     } } 

both below statements compile fine:

nestedgeneric<integer> test1 = new nestedgeneric<integer>(); nestedgeneric.innergeneric<string> test2  = new nestedgeneric.innergeneric<string>(); 

when nestedgeneric passed type parameter , constructor called, t? going same type parameter passed nestedgeneric?

in other words, can outer classes type parameters passed inner classes generic type declarations?

in other words, suppose question is, can outer classes type parameters passed inner classes generic type declarations?

no. there no relationship (like inheritance or field) between outer , inner static class. can create object of inner static class without dependency on outer class in example:

nestedgeneric.innergeneric<string> test2  = new nestedgeneric.innergeneric<string>(); 

however when use an instance of inner class field generic type derived outer class:

private innergeneric<t> innergenericinstance;  innergenericinstance = new innergeneric<t>(); 

a third variation define inner class field (non-static):

private class innergeneric<t> {     public t innergenericfield; } 

which type outer class since member variable.

as pointed out in comment defining both inner static & outer class type confuse reader (and @ later point in time). should declared different generic like

public class nestedgeneric<t> {     private innergeneric<t> innergenericinstance;      private static class innergeneric<u> {         private u innergenericfield;     }      nestedgeneric() {         innergenericinstance = new innergeneric<t>();     } } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -