How do i pass a class type into a function

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

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?

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

class Foo {
  Foo(){}
}

Foo bar = new Foo();

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

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
}

While I’m a little sceptical of your design, you might want to look at Class.isInstance(…) instead of instanceof.

1 Like

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

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;
}

Maybe Class cls

Chrisir

1 Like

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(..).

1 Like

That worked


// 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 };
  }
}

1 Like

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

I used this:

Did you try?

1 Like

oh i got it, thanks!

1 Like