# How do i pass a class type into a function

**URL:** https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337
**Category:** Coding Questions
**Created:** [May 28, 2021, 3:24pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337 "2021-05-28T15:24:42Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [May 28, 2021, 3:24pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/1 "2021-05-28T15:24:42Z")

</div>

maybe i’m going about this wrong, but i have a base class with a run function, and i want each child class to be able to pass a different class type into it when they use it.

the base class is called ‘spawner’, and each child class is a variant that spawns a different class of object. so i need to be able to pass that class type into the function.

what i have is a base class run function like

```auto
void run(what do i pass in here to get passedClass){
   if(someObject instanceof passedClass){
      someCode();
   }
}

```

essentially i am checking the objects that have already been spawned to see if they match the type of object i want to spawn. how do i get a reference to a class type, what type of variable even is that?

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [May 28, 2021, 5:03pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/2 "2021-05-28T17:03:49Z")

</div>

You’re in luck. You don’t need to pass in anything! Just define your class globally.

```auto
class Foo {
  Foo(){}
}

Foo bar = new Foo();

void run(){
  if( bar instanceof Foo ){
    print( "It's a foo, yes indeed!")
  }
}

```

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [May 28, 2021, 5:40pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/3 "2021-05-28T17:40:35Z")

</div>

but what if i want a function that takes a parameter to determine which class goes in place of Foo

a la  
void run(class){  
instanceof class  
}

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [May 28, 2021, 8:44pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/4 "2021-05-28T20:44:12Z")

</div>

While I’m a little sceptical of your design, you might want to look at [Class.isInstance(…)](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#isInstance-java.lang.Object-) instead of instanceof.

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [May 29, 2021, 1:28am UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/5 "2021-05-29T01:28:05Z")

</div>

okay ignoring the weird spawner design, say i want a function that uses instanceof or some similar comparison that checks the class type of an object. i want to use this function to look through an arraylist of objects and count ow many there are of a subclass. what would i pass into the function if i wanted to vary which kind of class type it counted. ie

```auto
int count(ArrayList<BaseClass> list,??? subclass /*what do i put here*/ ){
   int n = 0;
    for(int i = 0;i < list.size();i ++){
      if(list.get(i) instanceof subclass){
         n++;
      }
   }
   return n;
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 29, 2021, 4:48am UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/6 "2021-05-29T04:48:51Z")

</div>

Maybe Class cls

Chrisir

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [May 29, 2021, 6:21am UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/7 "2021-05-29T06:21:21Z")

</div>

As I said, pass in a `Class` object (which you can get from eg. `Foo.class`) and replace `instanceof` with a call to `cls.isInstance(..)`.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 29, 2021, 1:52pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/8 "2021-05-29T13:52:03Z")

</div>

That worked

```auto

// https://Discourse.Processing.org/t/
// parent-function-not-using-overwritten-array-from-child/22796/3

// https : // discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/5 

// parts by GoToLoop (2020/Jul/23)

final Parent[] children = {
  new Child1(), 
  new Child2(), 
  new Child3(), 
  new Child1(), 
  new Child1() 
};

void setup() {
  size(660, 660);
  printArray(children);
  println();
  for (final Parent child : children) {  
    println(child.isArrayTrue(4));
  }

  print("counting... "); 
  println(count(children, Child1.class ));
}

void draw() {
  //
}//

// ------------------------------------------------------------------------------------------

int count(Parent[] list, Class cls ) {
  int n = 0;

  for (int i = 0; i < list.length; i++) {
    if (cls.isInstance(list[i] ) ) {
      n++;
    }
  }
  return n;
}

//==========================================================================================

abstract class Parent {
  boolean[] array;

  boolean isArrayTrue(final int index) {
    return array[index];
  }

  @Override String toString() {
    return java.util.Arrays.toString(array);
  }
}

class Child1 extends Parent {
  {
    array = new boolean[] { true, true, true, true, true, true, true };
  }
}

class Child2 extends Parent {
  {
    array = new boolean[] { false, true, false, true, false, true, false };
  }
}

class Child3 extends Parent {
  {
    array = new boolean[] { false, true, true, false, true, true, false };
  }
}

```

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [May 30, 2021, 2:07pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/9 "2021-05-30T14:07:04Z")

</div>

this actually worked on the definition side but i don’t know how to reference a class to pass in apparently lol. so my definition now has Class cls in the parameter, and that works. but it wont just let me use the name of the class to pass in like count(list,Subclass) - it says The variable “Subclass” does not exist

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 30, 2021, 2:09pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/10 "2021-05-30T14:09:42Z")

</div>

> [@canslp](#):
>
> it wont just let me use the name of the class to pass in like count(list,Subclass)

I used this:

> [@Chrisir](#):
>
> `Child1.class`

Did you try?

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [May 30, 2021, 2:19pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-class-type-into-a-function/30337/11 "2021-05-30T14:19:10Z")

</div>

oh i got it, thanks!
