1

I am confused to undrestand when we should pass a class as an argument instead of its instance. for example:

myMethod(classA.class);

Could you make an example of when and how we should pass a class as an argument?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Majico
  • 3,810
  • 2
  • 24
  • 36

4 Answers4

9

A classic example is when creating an instance of a class through reflection:

//avoiding exception handling, leave that up to you
static <T> T make(Class<T> clazz) {
    return clazz.newInstance();
}
//...
A a = make(A.class);

Also, when you want to make sure at compile time that some references belong to a specific class, as used in Collections#checkedXxx.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Lets consider that we have some Creator

  abstract class Creator<T>
  {
    Creator(Class<T> c)
    {
        this.c = c;
    }

    T addMainElement(Object obj)
    {
        return c.cast(this);
    }

    private Class<T> c;
  }

And some wrapper

   class CreatorWrapper extends Creator<CreatorWrapper>
   {
      CreatorWrapper() {
        super(CreatorWrapper.class);
      }

      CreatorWrapper addMinorElement(Object obj)
      {
        return this;
      }
   }

The main advantage of doing this that way is that we can use our creator like that

   CreatorWrapper creator = new CreatorWrapper()
        .addMainElement(someObj1)
        .addMinorElement(someObj2);

We won't be able to do this if base class have no knowledge about child class.
And we won't be disturbed by "Unchecked cast from main.Creator to T" warning as we will be if we cast like that

  return (T)this;

See Java Class.cast() vs. cast operator

Community
  • 1
  • 1
Łukasz Kosiak
  • 513
  • 6
  • 22
1

For example,If you want to encapsulate many fields' value to an entity such as the Hibernate framework's method "session.get(Class entityClass,String primaryKey)" .You need to define the entityClass so that Hibernate know how to encapsulate the query result into an entity. A simple example to use Class as a argument:

 public T getInstance(Class<T extends Serializable> clazz) throws Exception
   {
          // the ParameterType "T extend Serializable" means that:
          // the argument clazz must be a sub of the Interface Serializable
          if(null != clazz)
           {
               return clazz.newInstacne();
           }

         return null;
    }
MageXellos
  • 154
  • 1
  • 9
0

Silly example:

public class PassClassExample {

    public static class ClassValidator {
        private Class theClass;
        public ClassValidator(Class theClass) {
            this.theClass = theClass;
        }

        public boolean instanceOf(Class someClass) {
            return theClass == someClass;
        }
    }

    public static void main (String [] args ) {
        ClassValidator personValidator = new ClassValidator(Person.class);

        Person you = new Person();
        Animal me = new Animal();

        System.out.println(personValidator.instanceOf(you.getClass()));
        System.out.println(personValidator.instanceOf(me.getClass()));

    }

    public static class Person {

    }

    public static class Animal {

    }
}

Will print out

true
false

Indicating you are a person and I am an animal :)

Ian2thedv
  • 2,691
  • 2
  • 26
  • 47