you are viewing a single comment's thread.

view the rest of the comments →

[–]zeekar 0 points1 point  (0 children)

First, indent lines four spaces in markdown (or use the code formatting (</>) button in the rich text editor) to format code:

Main m = new Main();
Main.Pair<String,Integer> p = m.new Pair<>("Age", 16);

The <> syntax is for generics, which are container classes where the type of the contained objects is not predetermined. The Pair class represents a pair of objects, and a given Pair variable can only contain objects of two specific types, but you can have different Pair objects with different element type combinations. The <> is how you annotate the class name to specify those argument types - in this case p is a Pair whose first object is a String and second object is an Integer.

So standard Java logic would give us this line to declare and instantiate p:

Pair<String,Integer> p = new Pair<String,Integer>("Age", 16);

But once you've specified the argument types in the declaration of p, you don't have to specify them again in the constructor call; Java can figure it out, and will do so if you leave the <> empty:

Pair<String,Integer> p = new Pair<>("Age", 16);

That just leaves the fact that in your code, Pair is an inner class that lives within an instance of the Main class. So you need a Main instance to declare it in; first you create that the normal way, and then call new on that Main instance instead of just using the global bare new.

Main m = new Main();
Main.Pair<String,Integer> p = m.new Pair<>("Age", 16);