Create class in Processing (error message)

I created a class in a separate TAB but I still get the “The class Test does not exist”. If I move the class code into the main tab it works fine. What do I need to do to import the class?

In main tab:

Test t1 = new Test();
t1.fname = "Per";

In the Test tab:

class Test{

  String fname;
  String lname;
  
  Test(){}

}

Hello @kmll,

I get an error with your class in a tab or in the main tab with Processing 4.1.3

Error:

No Error:

This works with the class in the tab or in the main tab:

void setup()
  {
  Test t1 = new Test();  // Here for testing.
  t1.fname = "Per";
  }

Resources here to explore:

There is a reference for class and Object with examples.

There is a tutorial for Objects.

:)

1 Like

Don’t name your class the same thing as the tab it’s in. While this is usually fine in most programming languages, I’ve seen it cause problems in Processing. Just try renaming your Test tab to TestClass, and see if that helps.

This happens because Processing is hiding details from you about how things are done behind the scenes.

2 Likes

Hi

Or the same name as the Sketch…!

1 Like

This is only a problem for the 1st main tab!

Other tabs can have a class or interface matching the same name as its tab.

P.S.: Actually, we can’t use the name of the 1st tab for a class or interface anywhere! :warning:

2 Likes

Also, the 2nd line must be in a function (setup())
and cannot stand alone outside of any function

1 Like

Thank you all for advise. Works fine now :slight_smile:

2 Likes