Usually, if there is no general type on the outer class, you can write code like this:
Outer outer = new Outer(); Inner inner = outer.new Inner();
because every method in the inner class already knows what types it should use.
But situations are a little more complicated if the outer class uses common types. Because the inner class has access to all members of its outer class (or classes), it must also be aware of the generic type used in the outer class (classes) to ensure type safety when manipulating shared values.
Take a look at this code:
class Outer<T> { private T someValue; class Inner { T getOuterValue(){ return someValue; } void setOuterValue(T value){ someValue=value; } }
This means that an instance of the Inner class also depends on the generic type on its outer class (es). That's why when creating a reference to an inner class, you need to explicitly use an outer class with a generic type, writing it as
Outer<String> outer = new Outer<>(); Outer<String>.Inner inner = outer.new Inner(); ^^^^^^^^^^^^^
or explicitly state that the outer class is using a raw type (which is discouraged) like
Outer.Inner inner = outer.new Inner();
So for your code to work, you need:
add an external class type (preferably with its common type)
call the constructor of the inner class on an instance of the outer class (just as non-static methods cannot be called without an instance, non-static (inner) classes must be created using an instance of the outer class)
InnerClassGenerics<String>.Innerclasss<String> innerclass = icg.new Innerclasss<>(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^ point 1 point 2
Also, you should not specify the same names for common types in nested classes and its outer classes , as in this case
public class InnerClassGenerics<T>{ class Innerclasss<T>{ ... } }
because T from Innerclasss hides T from its outer class InnerClassGenerics (not that it causes the current problem, but it can complicate your life later).