Can't reference inner class from a static context, but only if outer class is generic

Tom

The following won't compile:

class Outer<T> {
    class Inner {

    }

    static class Nested {
        Inner inner; // Error: Outer.this cannot be referenced from a static context
    }
}

However, if I remove <T>, it compiles. Why the inconsistency?

Also, if I say Outer.Inner inner;, instead of Inner inner;, it compiles. Again, why the inconsistency?

I'd expect an error in either all cases or none. Could anyone explain what's going on?

Blip

Why the inconsistency?

I would say this is not inconsistency at all. This is basically a problem of understanding of generics. Consider the following code (Your modified code):

class Outer<T> {
    class Inner{
        T t;//Added this extra line
    }

    static class Nested {
        Inner inner; 
    }
}

In this above example is somewhat similar to what you have written only I have added a new variable t of type T which is the generics of Outer class in the class Inner. Now in this above example would not compile because there is a non-static or runtime reference present in the Inner class, so, when you declare Inner in a static class Nested the JAVA complier does not know the type of T, which is only declared in the runtime, so you get an error. But in your case you have done nothing like that but still the compiler does not know whether something like that is present or not. So it gives the error.

Now in the second case you have removed the generic T from the class declaration of the Outer. So there is no possibility of declaring variable t in the Inner class so there is no error.

In the third case you declared Outer.Inner for the type of variable inner and it compiled successfully. Here the compiler considered Outer as RAW TYPE. But this type of raw type declarations should be avoided. So it would be better to write:

 Outer<?>.Inner inner;

Here Java compiler considers Outer to take any object as parameters which would inherit Object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I statically reference an inner class's static method on a generic class?

From Dev

Why can’t this static inner class call a non-static method on its outer class?

From Dev

Access generic outer class field from inner class without casting

From Dev

Accessing static methods of inner class from an outer class nonstatic constructor

From Dev

Why an Outer Class Can’t Be Static?

From Java

How can I get the outer class's reference if the anonymous Inner Class has two outer class?

From Dev

Why can't I access outer class from inner Thread class in Java?

From Dev

Can I call an inner class from the outer class's classmethod?

From Dev

How to reference outer object from inner class in Scala

From Dev

How to reference outer object from inner class in Scala

From Dev

How does static inner class can access all the static data members and static member function of outer class?

From Dev

Java: reference outer class in nested static class

From Dev

'Main.this' cannot be referenced from a static context if outer class is generified

From Dev

Nested class AsyncTask can't modify Outer Class static objects

From Dev

Inner class generic type same as outer type

From Dev

java nested class inner and outer reference equals

From Dev

Accessing a field of outer class from inner class

From Dev

referencing the App context from a non-static inner class

From Dev

referencing the App context from a non-static inner class

From Dev

Java - 'Name clash' and 'constructor undefined' error when overriding inner class that inherits a generic from the outer class

From Dev

Inner Class has an implicit reference to the outer class and may can leak memory

From Dev

How do I reference the "display" variable from my inner class, to use in the outer class in my JLabel

From Dev

How to call a static method on a base class from a generic class<T>?

From Dev

java best practice -outer class static variable accessed by inner class

From Dev

C++: static instance of inner class in outer class

From Dev

Accessing a method of the Outer Class inside a static Inner Class

From Dev

Can't access outer class from anonymous class

From Dev

Can't access outer class from fragment class android

From Dev

Can't access outer class from fragment class android

Related Related

  1. 1

    Why can't I statically reference an inner class's static method on a generic class?

  2. 2

    Why can’t this static inner class call a non-static method on its outer class?

  3. 3

    Access generic outer class field from inner class without casting

  4. 4

    Accessing static methods of inner class from an outer class nonstatic constructor

  5. 5

    Why an Outer Class Can’t Be Static?

  6. 6

    How can I get the outer class's reference if the anonymous Inner Class has two outer class?

  7. 7

    Why can't I access outer class from inner Thread class in Java?

  8. 8

    Can I call an inner class from the outer class's classmethod?

  9. 9

    How to reference outer object from inner class in Scala

  10. 10

    How to reference outer object from inner class in Scala

  11. 11

    How does static inner class can access all the static data members and static member function of outer class?

  12. 12

    Java: reference outer class in nested static class

  13. 13

    'Main.this' cannot be referenced from a static context if outer class is generified

  14. 14

    Nested class AsyncTask can't modify Outer Class static objects

  15. 15

    Inner class generic type same as outer type

  16. 16

    java nested class inner and outer reference equals

  17. 17

    Accessing a field of outer class from inner class

  18. 18

    referencing the App context from a non-static inner class

  19. 19

    referencing the App context from a non-static inner class

  20. 20

    Java - 'Name clash' and 'constructor undefined' error when overriding inner class that inherits a generic from the outer class

  21. 21

    Inner Class has an implicit reference to the outer class and may can leak memory

  22. 22

    How do I reference the "display" variable from my inner class, to use in the outer class in my JLabel

  23. 23

    How to call a static method on a base class from a generic class<T>?

  24. 24

    java best practice -outer class static variable accessed by inner class

  25. 25

    C++: static instance of inner class in outer class

  26. 26

    Accessing a method of the Outer Class inside a static Inner Class

  27. 27

    Can't access outer class from anonymous class

  28. 28

    Can't access outer class from fragment class android

  29. 29

    Can't access outer class from fragment class android

HotTag

Archive