1

Is it possible to access a getter from a class out of a List of classes? See my code:

List<Object> words = getWorld().getObjects(Word.class);
for (Object word : words) {
    if (word.getWord() == inputText) {
        System.out.println("Test");
    }

}

When compiling it says: cannot find method getWord().

Thanks in advance!

Mureinik
  • 297,002
  • 52
  • 306
  • 350
wderoode
  • 63
  • 1
  • 5
  • This depends on whether the `getWorld().getObjects` method can be parameterized to return a `List` of `Word` objects based on the class instance sent to `getObjects`. Can you post some code for the type returned by `getWorld()`? – Invisible Arrow Jan 17 '15 at 19:23

1 Answers1

1

Hopefully, getObject(Class<T>) would return a List<T>. In that case, you could define words as List<Word>:

List<Word> words = getWorld().getObjects(Word.class);
for (Word word : words) {
    // rest of the code.

If this is not possible, you'd have have to cast it:

List<Object> words = getWorld().getObjects(Word.class);
for (Object word : words) {
    // Probably a good idea to check word's type before casting
    if (((Word) word).getWord() == inputText) {
Mureinik
  • 297,002
  • 52
  • 306
  • 350