I am building an Apex Web Service which will be called from an external mobile application. Because the nature of this application is to go in and out of Salesforce1 (to the other external mobile app), I'd like to retain a "state variable" each time the application calls the Apex code. Can I use a global static variable to retain the state information or is there a better way to handle this in Apex? I'm happy to provide more details about the process if that helps clarify the question.
In Apex, you can not hold a variable between different transactions programatically. But there is another way which we call Custom Settings.
So you can save that value in custom setting which can be queried in any transaction and can be modified if required. since this is stored in database, it won't get affected.
There are two types of custom settings, List and Hierarchy. Custom Settings.