incompatible type wile returning generic method of statictype

user3878073 :

I tried to implement a BST class was like

class BST<T> {
   private Node<T> root;
   private static class Node<T> {
     T element;
     Node<T> left;
     Node<T> right; 
   }  
}

I want to get the successor node in a method which takes a Node as a parameter and returns a successor node

private <T> Node<T> get(Node<T> current node){  
    Node<T> successor = root; 
}

here I'm getting incompatible types . if I remove the method bound

private Node<T> get(Node<T> current node) {
    Node<T> successor = root;
}

now it is compiling fine. What is the reason?

mkjh :

reason is T has already been defined at class level BST<T>. which means that you have to use BST<String>.method(); If you remove <T> from BST, the type will then have to be specified at the method level and you will need to add <T> at your method declaration. And calling the method will be BST.method<String>();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=106767&siteId=1