theutonium.18
theutonium.18

Reputation: 525

Resolving hidden Type Parameter in Java

Consider the following code:

class Scratch<T> {
  class InnerClass<T> {
    public void executeHiddenMethod(){
     //..some code to use Inner (T) type
     T r = null; //declared T from inner T type
     //..some code to use Outer (T) type
     //?? How to use outer T type?
    }
  }


//when trying to invoke the code:
public static void main(String[] args) {
    Scratch<String> scr = new Scratch<>();
    Scratch<String>.InnerClass<Double> d = scr.new InnerClass<>();
    d.executeHiddenMethod();
  }
}

Upvotes: 1

Views: 78

Answers (1)

radof
radof

Reputation: 611

Unless I misunderstood the question, you should be able to use a different type parameter for the outer and inner class. T for Scratch and S for InnerClass.

 class Scratch<T> {
      class InnerClass<S> {
        public void executeHiddenMethod(){
         ...
         S s = null; 
         ...
         T t = null;
        }
      }
}

Upvotes: 1

Related Questions