Skip to main content

Posts

Showing posts with the label Singleton

How to serialize a class with static variables in Java

TL;DR You can't! Instead; Change the static variables into a none static member variable of a singleton class. By making the variables holding class to a singleton, still, it caters to the requirement of keeping your variables having a single reference for the entire application. (This is one of the ways of doing it, surely there are more other elegant ways and crude hacks of doing this depending on the situation) This is a problem that a scholar asked me for some help with. We have a simple use case where there's a configuration class that it requires to hold some values used in application-wide. So the easiest way (not the best) somebody could think would be making the variables static, so it would be only a single reference for it throughout the whole application. Let's imagine the configuration class would look like below. import java.io.Serializable ; public class BranchConfig implements Serializable { private static String branchNa...