Results 1 to 5 of 5
  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default About generic, cant use method

    Java Code:
    public class Co1 {
    
    	public void print()
    	{
    		System.out.println("Co1");
    	}
    }
    Java 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();
    	}
    	
    }
    Err output
    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)

  2. #2
    popeus is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default 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.
    Java Code:
    Co1 lolol = new Co1;
    Co1.print();
    I have absolutely no Idea what your trying to do with this class so I don't quite understand
    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

  3. #3
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default 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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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:

    Java Code:
    Holder3<String> stringHolder = new Holder3<String>();
    stringHolder.get();
    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.

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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:

    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();
       }
    
    }
    This will give all T variables access to all public Co1 methods.

Similar Threads

  1. Generic method invocation Clarification required
    By muzu1232005 in forum Advanced Java
    Replies: 7
    Last Post: 04-24-2011, 10:59 PM
  2. Using a Generic Insertion Sort method
    By dubois.ford in forum New To Java
    Replies: 0
    Last Post: 02-06-2011, 03:08 AM
  3. Generic method
    By jomypgeorge in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 05:51 AM
  4. Generic method problem
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 10-24-2010, 06:30 PM
  5. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •