Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference the generic type of a member of an inner class?

I'm curious to understand why testInnerClass fails to compile, citing:

incompatible types: Object cannot be converted to String.

import java.util.List;

class Test<
        I extends Test.InnerClass,
        S extends Test.StaticInnerClass,
        O extends OtherClass> {

    void testOtherClass(O other) {
        String firstString = other.strings.get(0); //this works
    }
    
    void testStaticInnerClass(S staticInner) {
        String firstString = staticInner.strings.get(0); //this works
    }
    
    void testInnerClass(I inner) {
        String firstString = inner.strings.get(0); //this fails:
        //"incompatible types: Object cannot be converted to String" 
    }

    static class StaticInnerClass {
        List<String> strings;
    }
    
    class InnerClass {
        List<String> strings;
    }
}

class OtherClass {
    List<String> strings;
}

testStaticInnerClass and testOtherClass work as I would expect but I'm not exactly sure why testInnerClass fails.

like image 470
Jota Avatar asked Mar 14 '21 15:03

Jota


People also ask

Can Java inner class be generic?

If I have one class (Class1) which takes the type E and also has an internal class (Class2) which I also want to take type E which should be the same as the E of Class1 in all cases. Class2 is a private internal class, so it will only ever be used by instances of Class1; which means it will never ever have any other E.

How do you access members of an inner class?

Accessing the Private Members Write an inner class in it, return the private members from a method within the inner class, say, getValue(), and finally from another class (from which you want to access the private members) call the getValue() method of the inner class.

How do I get a class instance of generic type T?

The short answer is, that there is no way to find out the runtime type of generic type parameters in Java. A solution to this is to pass the Class of the type parameter into the constructor of the generic type, e.g.

How do you declare a generic type in a class explain?

If we want the data to be of int type, the T can be replaced with Integer, and similarly for String, Character, Float, or any user-defined type. The declaration of a generic class is almost the same as that of a non-generic class except the class name is followed by a type parameter section.


1 Answers

InnerClass is an inner class of Test which expects generic parameters. So you need to update the class declaration as:

class Test<
        I extends Test<I,S,O>.InnerClass,
        S extends Test.StaticInnerClass,
        O extends OtherClass>

The StaticInnerClass, even though inside Test, it is declared static.So as every static method or variable, the static class also does not depend on any state of the class. Hence it is not required to have S extends Test<I,S,O>.StaticInnerClass

like image 185
Gautham M Avatar answered Oct 21 '22 04:10

Gautham M