Can parameterizing common inner classes be used?

package generics; public class InnerClassGenerics{ class Innerclasss{ } public static void main(String[] args) { InnerClassGenerics icg=new InnerClassGenerics(); Innerclasss innerclass=icg.new Innerclasss(); } } 

Above code is possible and compiles fine !!!


Why is the code below not compiling and is it possible to parameterize inner classes?

 package generics; public class InnerClassGenerics<T>{ class Innerclasss<T>{ } public static void main(String[] args) { InnerClassGenerics<String> icg=new InnerClassGenerics<>(); Innerclasss<String> innerclass=new Innerclasss<>(); } } 

In the above code, if the class is made as static, it works fine !!! Why is this impossible without a static keyword?

+2
source share
2 answers

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; } } //rest of code } 

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).

+4
source

So, you have this class definition, and it will not compile, because you have 2 types called T Do it like this and compile:

 public class OuterClass <O> { public class InnerClass <I> { } } 

Also note that you are creating a non-static class from a static method. The full code should look like this:

 public class OuterClass <O> { public static class InnerClass <I> { } public OuterClass () { } public InnerClass<O> getInnerClass () { return new InnerClass<O>(); } public static void main (String[] args) { OuterClass<String> outer = new OuterClass<>(); InnerClass<String> inner = outer.getInnerClass(); } } 

This code requires Java 8 to compile.

-1
source

Source: https://habr.com/ru/post/1208782/


All Articles