Serialization in P4

I’m trying to use java object serialization in P4 and have a problem. I have a class which implements Serializable, but I get an error on execution of “NotSerializable”. I read somewhere (here?) that a serializable class needs to be in its own tab and the tab should be named “name.java” (rather than just “name”). That now gets the serialization to work fine (I had to import java.io.* in this tab now) until I use (for instance) the ‘split’ function which is now not found.
I’m obviously missing something in my bid to use object serialization in P4. Any hints?

Try adding the following import to the serializable class

import processing.core.*;
1 Like

Tried your suggestion: Split and other P4 functions still “do not exit”. It seems like I have to use pure java e.g., String.split(String) rather than the simpler P4 version?

That should be “do not exist”

The Processing method split is a static member of the PApplet class, so as well as using the import

import processing.core.*;

you would call the method using

PApplet.split(...);

1 Like

Thank you! All good now.

As an academic issue (now), why does serialization not work in a normal .pde tab?

When a class is defined inside the pde tab it becomes an inner class to the main sketch class (named after the sketch). As an inner class it has access to all the imports, variables and functions in the sketch class.

If we create a class called Foo and put it into a tab called Foo.java it will be saved in a seperate file also called Foo.java. This is then a top-level class and does not have access to the inner workings of the sketch class.

It is possible to promote an inner class to a top level class in a pde tab like this

main_sketch.pde

void setup(){
   ...
}

void draw(){
  ...
}

public static class Foo implements Serializable { 

}

but it does not have the same access privileges as an inner class.

Should work but I have not tried this with Serializable as I almost always create my classes as top-level i.e. in their own file.

1 Like

That’s what happened on this old issue:

Solution is either declare class as static or place the class inside its own “.java” file tab:

2 Likes