I have the following class:
public class Town implements Serializable {
public String name = "Town";
public String color = "White";
public ArrayList<UUID> mayors = new ArrayList<>();
public ArrayList<UUID> cabinet = new ArrayList<>();
public ArrayList<UUID> citizens = new ArrayList<>();
}
I want to add an "addPlayer" void, that adds the UUID of a player, (working on a spigot/minecraft plugin) obtained from Player.getUniqueID() (returns a UUID object), I'm wondering if my method could look like this:
public void addPlayer (Player p){
citizens.add(p.getUniqueID());
}
or if since the Player object is not serializable if I would need to just have the method take a UUID instead of a player?
Simply put, can non-serializable objects be used within the scope of functions since they aren't being used as field variables?
I know it'd be super easy to just put in a UUID instead of player object if necessary, but knowing whether or not I can do this will help me a lot with some other things I have planned for the project
Yes you can.
Serialize is only concerned with the state of an object, which is determined by the instance varibles and not by its methods.
To use non-serializable objects within the Serializable class without affecting serializabilty of the class itself like in your case; your Player
obj will only used within the scope of the addPlayer
method. Its not stored as an instance variable on your Town
class so it does not really affect the serializabilty of your Town
class. So yes you can use your addPlayer(Player p)
method as is. The only thing that gets serialized are the fields of your Town class such as (name, color, mayors, cabinet, citizens)
. So no need to worry about your Player
not being serializable.