8

My problem is that I am using a class not developed by me (I took it from Microsoft Azure SDK for Java). The class is called Node and you can see it here.

As you can see the class is a generic class declared recursively like this:

public class Node<DataT, NodeT extends Node<DataT, NodeT>> {
      ...
}

When I try to instantiate it I don't know how to do it. I am doing this but I know IT IS NOT the way because it has no end:

Node<String, Node<String, Node<String, Node<...>>>> myNode = new Node<String, Node<String, Node<String, Node<...>>>>;

I hope you understand my question. Thanks.

2
  • May declaring it abstract would clarify Commented May 10, 2017 at 16:09
  • @ArneBurmeister Exactly my point! It doesn't help at all that the Node class is not abstract. Commented May 11, 2017 at 0:54

3 Answers 3

5

One way is to extend Node like:

class MyNode<T> extends Node<T, MyNode<T>> {
}

and then instantiate it like:

Node<String, MyNode<String>> node1 = new MyNode<String>();

or

MyNode<Integer> node2 = new MyNode<Integer>();
Sign up to request clarification or add additional context in comments.

1 Comment

Fine and flexible way.
4

You have to declare a class which extends Node so you can use the name of the class:

class StringNode extends Node<String, StringNode> {
}

Comments

2

You can use a wildcard in your variable declaration:

Node<String, ?> n = new Node<>();

Or you can create an explicit subclass

class StringNode extends Node<String, StringNode> { }

and instantiate it via

Node<String, ?> n = new StringNode();

or

StringNode n = new StringNode();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.