Results 1 to 5 of 5
Thread: About generic, cant use method
- 09-13-2011, 07:00 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 20
- Rep Power
- 0
About generic, cant use method
Java Code:public class Co1 { public void print() { System.out.println("Co1"); } }Err outputJava Code:public class Holder3<T> { private T a; public Holder3(T a) { this.a = a; } public void set (T h4) { this.a = h4; } public T get() { a.print(); [COLOR="#FF0000"][B][U]//here, why cant't i use the method print() in class Co1[/U][/B][/COLOR] return a; } public static void main (String[] args) { Holder3 h3 = new Holder3 (new Co1() ); h3.get(); } }
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method print() is undefined for the type T at Holder3.get(Holder3.java:16) at Holder3.main(Holder3.java:25)
- 09-13-2011, 07:11 AM #2
Member
- Join Date
- Sep 2011
- Posts
- 12
- Rep Power
- 0
Re: About generic, cant use method
referencing a non static method in a static context I believe?
make an object of the Co1 class eg.
I have absolutely no Idea what your trying to do with this class so I don't quite understandJava Code:Co1 lolol = new Co1; Co1.print();
why you're trying to call a.print(); but the above works for getting rid of the error you're talking
about.Last edited by popeus; 09-13-2011 at 07:12 AM. Reason: all dem semicolums
- 09-13-2011, 07:22 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 20
- Rep Power
- 0
Re: About generic, cant use method
hi popeus ,
Thank for your reply.
I just learned the chapter generic following a book.
I want to do some test to understand the content.
The code should act like that :
pass a Co1 reference to generic class Holder3, and the T should be the Co1 type, and a should be the instantized Co1, so a should have the method print() in class Co1 ,but it does not work. So confused.Last edited by oszc; 09-13-2011 at 07:24 AM.
- 09-13-2011, 05:34 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: About generic, cant use method
Popeus' help while meant to be helpful, was incorrect. The problem is that T is not Co1, T is some type which is not known as you are writing the code.
Assume for a moment that 'a.print()' was legal, then what would happen if I threw this wrench into the equation:
When you create a generic class you are writing for a generic type. It's not known until compilation; and furthermore you can't query the object easily to find information about the generic type(but this is off topic). When you declare and initialize the generic class, you tell it what type T represents, think for a moment of how to use classes such as ArrayList, you usually(and should always be) using generic parameters: ArrayList<String>, ArrayList<Integer>, etc.Java Code:Holder3<String> stringHolder = new Holder3<String>(); stringHolder.get();
So you can't have methods being called on the generic type unless you can ensure that any class which can be passed in as a generic argument, in fact contains the method.
If you'd like a print description, I'd suggest you supply an overloaded toString method and use System.out.println(Object o);
Java Code:public class Co1{ @Override public String toString(){ .... } } public class Holder3<T>{ private T a; @Override public String toString{ return a.toString(); } public T get(){ System.out.println(this); return a; } }Last edited by sunde887; 09-13-2011 at 05:36 PM.
-
Re: About generic, cant use method
A solution would be to have your generic variable T be constrained to be only the Co1 class or its child classes:
This will give all T variables access to all public Co1 methods.Java Code:class Holder3<T extends Co1> { private T a; public Holder3(T a) { this.a = a; } public void set(T h4) { this.a = h4; } public T get() { a.print(); return a; } public static void main(String[] args) { Holder3 h3 = new Holder3(new Co1()); h3.get(); } }
Similar Threads
-
Generic method invocation Clarification required
By muzu1232005 in forum Advanced JavaReplies: 7Last Post: 04-24-2011, 10:59 PM -
Using a Generic Insertion Sort method
By dubois.ford in forum New To JavaReplies: 0Last Post: 02-06-2011, 03:08 AM -
Generic method
By jomypgeorge in forum New To JavaReplies: 6Last Post: 01-18-2011, 05:51 AM -
Generic method problem
By counterfox in forum New To JavaReplies: 6Last Post: 10-24-2010, 06:30 PM -
standard input stream storing to a generic method?
By vendetta in forum New To JavaReplies: 3Last Post: 01-29-2010, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks