Using object type as variable

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:

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):

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

I believe you should check Java’s keyword instanceof:
forum.Processing.org/two/discussions/tagged/instanceof

1 Like

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.

  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.

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

1 Like

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

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

It seems to work, thank you!