# Serialization in P4

**URL:** <https://discourse.processing.org/t/serialization-in-p4/42420>\
**Category:** Beginners\
**Created:** [July 14, 2023, 12:51pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420 "2023-07-14T12:51:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marek](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@marek](https://discourse.processing.org/u/marek)\
**Post date:** [July 14, 2023, 12:51pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/1 "2023-07-14T12:51:56Z")

</div>

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](http://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?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 14, 2023, 1:26pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/2 "2023-07-14T13:26:48Z")

</div>

Try adding the following import to the serializable class

```auto
import processing.core.*;

```

---

<div class="post-metadata">

**Author:** ![marek](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@marek](https://discourse.processing.org/u/marek)\
**Post date:** [July 14, 2023, 2:05pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/3 "2023-07-14T14:05:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![marek](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@marek](https://discourse.processing.org/u/marek)\
**Post date:** [July 14, 2023, 2:05pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/4 "2023-07-14T14:05:59Z")

</div>

That should be “do not exist”

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 14, 2023, 2:20pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/5 "2023-07-14T14:20:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marek](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@marek](https://discourse.processing.org/u/marek)\
**Post date:** [July 14, 2023, 4:31pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/6 "2023-07-14T16:31:27Z")

</div>

Thank you! All good now.

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 14, 2023, 5:12pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/7 "2023-07-14T17:12:25Z")

</div>

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`**

```auto
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.

---

<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:** [July 14, 2023, 10:27pm UTC](https://discourse.processing.org/t/serialization-in-p4/42420/8 "2023-07-14T22:27:37Z")

</div>

> [@quark](#):
>
> When a class is defined inside the pde tab it becomes an `inner class` to the main sketch class (named after the sketch).

That’s what happened on this old issue:

> [@Deserializing objects sent from a client program to a server program](https://discourse.processing.org/t/deserializing-objects-sent-from-a-client-program-to-a-server-program/16124/9):
>
> Yes, your sketch works but because it is serializing and deserializing whithin the same scketch and thus class But what I’m having problems with is having 2 programs talking to each other (deserializing the data recived), the rest is good But when I try to deserialize java complains because the classes are different even tough they share the same source code I tried using .java files to make processing make the class without the .pde so it would give me ImagenSerializable and not NCliente$Im…

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

> [@LinkedList vs ArrayList](https://discourse.processing.org/t/linkedlist-vs-arraylist/19946/14):
>
> Here’s a more streamlined FiFo Queue container I’ve come up w/ just now: innocent[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html) /\*\* \* ArrayQueue (v1.1.1) \* GoToLoop (2020/Apr/20) \* https://Discourse.Processing.org/t/linkedlist-vs-arraylist/19946/14 \*/ //package java.util; // Uncomment this line if this is a ".java" file import java.util.Collection; import java.util.Queue; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import ja…

> <https://gist.github.com/GoToLoop/145418d1dfb062c7c41a630bce5b8374>
>
> There are more than three files. show original
