4

I'm having a hard time wrapping my head around Java generic types. Here's a simple piece of code that in my mind should work, but I'm obviously doing something wrong.

Eclipse reports this error in BreweryList.java:

The method breweryMethod() is undefined for the type <T>

The idea is to fill a Vector with instances of objects that are a subclass of the Brewery class, so the invocation would be something like:

BreweryList breweryList = new BreweryList(BrewerySubClass.class, list);

BreweryList.java

package com.beerme.test;

import java.util.Vector;

public class BreweryList<T extends Brewery> extends Vector<T> {
    public BreweryList(Class<T> c, Object[] j) {
        super();
        for (int i = 0; i < j.length; i++) {
            T item = c.newInstance();

            // breweryMethod() is an instance method
            // of Brewery, of which <T> is a subclass (right?)

            c.breweryMethod();

            // "The method breweryMethod() is undefined
            // for the type <T>"
        }
    }
}

Brewery.java

package com.beerme.test;

public class Brewery {

    public Brewery() {
        super();
    }

    protected void breweryMethod() {
    }
}

BrewerySubClass.java

package com.beerme.test;

public class BrewerySubClass extends Brewery {
    public BrewerySubClass() {
        super();
    }

    public void brewerySubClassMethod() {
    }
}

I'm sure this is a complete-generics-noob question, but I'm stuck. Thanks for any tips!

3
  • 2
    A strongly suggest using your own BreweryFactory instead of abusing Class. If you can reasonably avoid reflection, do so. Commented Mar 16, 2010 at 19:12
  • 4
    stackoverflow.com is much better for programming related questions. Commented Mar 16, 2010 at 19:32
  • Are you sure the line "c.initBreweryFromObject();" isn't "item.initBreweryFromObject();"? Assuming so -- Commented Mar 17, 2010 at 7:54

4 Answers 4

9
item.breweryMethod();

instead of

c.breweryMethod();

You were attempting to call the method on the Class<T>, not on the instance.

And actually, Eclipse reports

The method breweryMethod() is undefined for the type Class<T>

and it should be clear from this message what has happened.

(And after you fix that, you will also have to handle the exceptions declared by Class.newInstance())

As Tom Hawtin suggested in a comment, you'd better use a factory method instead of using reflection in your method, in order to create your object.

Sign up to request clarification or add additional context in comments.

1 Comment

Ach! You're right, of course: item instead of c. It compiles now after adding appropriate "throws" clauses. I'll investigate factory methods. Thanks for the tip.
4

Don't you mean:

item.breweryMethod();

instead of:

c.breweryMethod();

?

Comments

2

First of all, to answer your question: In the loop it should be item.breweryMethod(); instead of c.breweryMethod();

I do not know what you plan to do, but it does not seem to be the best approach. Here are some suggestions how you could improve your code:

  • Replace Vector with ArrayList if you do not need the list to be synchronized
  • Instead of extending a List implementation, consider using a list, or does your BreweryList provide more functionality than a standard list?

1 Comment

BreweryList will provide more functionality, as it's an interface between a remote MySQL database and a smartphone display. I'll take a look at ArrayList; that might in fact work better. Thanks.
1

Aside from the probable typo mention above... I don't see any such method initBreweryFromObject() defined anywhere in your sample code. This is the error then, no?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.