# Using object type as variable

**URL:** https://discourse.processing.org/t/using-object-type-as-variable/30531
**Category:** Coding Questions
**Created:** [June 6, 2021, 2:58pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531 "2021-06-06T14:58:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![StainlessHolz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stainlessholz/32/13542_2.png) [@StainlessHolz](https://discourse.processing.org/u/StainlessHolz)
#### Post date: [June 6, 2021, 2:58pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531/1 "2021-06-06T14:58:37Z")

</div>

Hi all,

I´ve made a program where the user can manipulate data with nodes. You can add input, output and different manipulation nodes.  
It is like the shader editor from Blender or the program whith which you can program the Siemens Logo PLC.  
It works for now but i think it is running very inefficient.

The structure is simply said a bunch of nodes wich are connected via “Interconnections” which take data frome one node and send it to other ones.  
The nodes are saved as classes in different ArrayLists depending on which node it is.  
The interconnections are also classes in an ArrayList which store the sending and the receiving class type as a string.

And now to my question:  
Is it possible to store class types in a variable?  
Something like:

```auto
Node obj = new Node();
classType ct = obj.getClass();

```

I think it can be helpful so i wouldnt have to compare strings all the time like in my code (just asnippet, original code is way too long):

```auto
for(int i = 0; i<Interconnections.size();i++){
      String dataBuffer = "";
      Interconnection Ic = Interconnections.get(i);
      //--------------------------------------------- from here get the data from nodes
      if(Ic.incomingNodeKind.equals("EthernetInputNode")){
        for(int j = EthernetInputNodes.size()-1; j>=0;j--){
          EthernetInputNode Node = EthernetInputNodes.get(j);
          if(Node.ID == Ic.incomingNodeID){
            dataBuffer = Node.sendIcData(Ic.incomingNodeLine);
          }
        }
      }
      else if(Ic.incomingNodeKind.equals("EthernetOutputNode")){
        for(int j = EthernetOutputNodes.size()-1; j>=0;j--){
          EthernetOutputNode Node = EthernetOutputNodes.get(j);
          if(Node.ID == Ic.incomingNodeID){
            dataBuffer = Node.sendIcData(Ic.incomingNodeLine);
          }
        }
      }
      else if(Ic.incomingNodeKind.equals("MathNode")){
        for(int j = MathNodes.size()-1; j>=0;j--){
          MathNode Node = MathNodes.get(j);
          if(Node.ID == Ic.incomingNodeID){
            dataBuffer = Node.sendIcData(Ic.incomingNodeLine);
          }
        }
      }
      //--------------------------------------------- from here send the data to nodes
      if(Ic.outgoingNodeKind.equals("EthernetInputNode")){
        for(int j = EthernetInputNodes.size()-1; j>=0;j--){
          EthernetInputNode Node = EthernetInputNodes.get(j);
          if(Node.ID == Ic.outgoingNodeID){
            Node.receiveIcData(Ic.outgoingNodeLine, dataBuffer);
          }
        }
      }
      else if(Ic.outgoingNodeKind.equals("EthernetOutputNode")){
        for(int j = EthernetOutputNodes.size()-1; j>=0;j--){
          EthernetOutputNode Node = EthernetOutputNodes.get(j);
          if(Node.ID == Ic.outgoingNodeID){
            Node.receiveIcData(Ic.outgoingNodeLine, dataBuffer);
          }
        }
      }
      else if(Ic.outgoingNodeKind.equals("MathNode")){
        for(int j = MathNodes.size()-1; j>=0;j--){
          MathNode Node = MathNodes.get(j);
          if(Node.ID == Ic.outgoingNodeID){
            Node.receiveIcData(Ic.outgoingNodeLine, dataBuffer);
          }
        }
      }
    }

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 6, 2021, 3:19pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531/2 "2021-06-06T15:19:17Z")

</div>

I believe you should check Java’s keyword `instanceof`:  
[forum.Processing.org/two/discussions/tagged/instanceof](http://forum.Processing.org/two/discussions/tagged/instanceof)

---

<div class="post-metadata">

### Author: ![StainlessHolz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stainlessholz/32/13542_2.png) [@StainlessHolz](https://discourse.processing.org/u/StainlessHolz)
#### Post date: [June 6, 2021, 3:38pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531/3 "2021-06-06T15:38:42Z")

</div>

Thanks, I think `instanceof` is part of the solution since i need to compare the class types.  
But before i can do that i need to store the class type and compare it later.

I have the class “Interconnection” and in it i would like to store the class type of a different class. But i dont know yet which it is.

```auto
  Interconnection(String iNK, int iNL, int iNI, String oNK, int oNL, int oNI){
    incomingNodeKind = iNK; //this is where one class type is stored
    incomingNodeLine = iNL;
    incomingNodeID = iNI;
    outgoingNodeKind = oNK; //this is where one class type is stored
    outgoingNodeLine = oNL;
    outgoingNodeID = oNI;
  }

```

So i would like to exchange the Strings with class types.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [June 6, 2021, 3:47pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531/4 "2021-06-06T15:47:44Z")

</div>

Keyword `instanceof` can’t use strings. Instead we compare an object w/ a class name type.

---

<div class="post-metadata">

### Author: ![StainlessHolz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stainlessholz/32/13542_2.png) [@StainlessHolz](https://discourse.processing.org/u/StainlessHolz)
#### Post date: [June 6, 2021, 4:03pm UTC](https://discourse.processing.org/t/using-object-type-as-variable/30531/5 "2021-06-06T16:03:44Z")

</div>

I didnt realise that there is type `Class` in which i can store the class type.  
My solution is this:

```auto
MathNode nodeClass = new MathNode();
Class nodeClassType = nodeClass.getClass();

```

It seems to work, thank you!
