Results 1 to 5 of 5
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default Class<T> in method parameter

    Hello,

    I want to call a method which takes an argument of type Class<T>.
    How can I call that method? I can't figure out how to create a Class<T> object.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You need to pass it (a reference to) an instance of class Class. You can get these with Object's getClass() method. Or use .class with a class literal like the parameter used with the allOf() method of EnumSet below:

    Java Code:
    import java.util.EnumSet;
    
    public enum ClassParamEg {
        ONE, TWO, THREE, FOUR, FIVE, SIX;
    
        public static final EnumSet<ClassParamEg> primes = EnumSet.of(TWO, THREE, FIVE);
        public static final EnumSet<ClassParamEg> composites = EnumSet.complementOf(primes);
        public static final EnumSet<ClassParamEg> all = EnumSet.allOf(ClassParamEg.class);
    	
        public static void main(String[] args) {
            System.out.print("all: ");
            for(ClassParamEg cpe :ClassParamEg.all) {
                System.out.print(cpe + " ");
            }
            System.out.println();
        }
    }

  3. #3
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    If I call .getClasS() on an object, it tells me I need to pass an argument of type Class<T> and not of type Class<capture#438 of ? extends object's-class>.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Perhaps it might help if you post a brief example illustrating the problem.

  5. #5
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    I have a method:
    Java Code:
    public <T extends Element> T findElement(Class<T> elementClass);
    But I can't manage to call it, because I can't get the argument type right.

Similar Threads

  1. Creating array in method parameter
    By Dipke in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 10:18 AM
  2. Replies: 4
    Last Post: 06-18-2010, 11:08 AM
  3. Replies: 10
    Last Post: 06-12-2009, 07:41 PM
  4. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 10:26 PM
  5. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 07:24 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
  •