"Main.this" cannot refer to a static context if an outer class is generated

Why is the following code ok, but as soon as T is added to Main as a generic, it throws the following error?

"Main.this" cannot refer to a static context

//public class Main<T> { - uncomment this for the error to appear public class Main { public static void main(String[] args) { new Main(); } class TestNonStatic {} private static class TestStatic { public TestStatic(TestNonStatic nonStatic) { //this is the line that fails } } } 

I have passed general restrictions , but I do not understand why this leads to an error. I also looked at a lot of similar issues, but I don't understand why adding a generic parameter will make a difference.

+6
source share
1 answer

Your TestNonStatic has an implicit pedigree that you must indicate if it is a raw type

  public TestStatic(Main.TestNonStatic nonStatic) { 

or general type

  public TestStatic(Main<String>.TestNonStatic nonStatic) { 

or using a non-static class

 private class TestStatic { public TestStatic(/*Main<T>.*/TestNonStatic nonStatic) { 

It will not imply the following, since the class is static

  public TestStatic(Main<T>.TestNonStatic nonStatic) { 

Why I don't have a default behavior, perhaps because it can lead to even more obscure error messages;)

+4
source

All Articles