[SalesForce] Single Apex Class for Multiple controller classes

I have one Apex Class. I want to access that Apex Class variable from other controller classes. If I'll change the value from any controller, then that same value should be available to all controller.

This is my Main CLass …..

                    Class public virtual class GenericControllerClass1 {
public static integer A{get;set;}
public GenericControllerClass1(){

    System.debug('$$$$$$$$$$$$$$ Main Class%%%%%%%%%%%%%'+ A);
}

}

My First Controller is ……

public class TestControllerClass1 extends GenericControllerClass1{ 
public TestControllerClass1(){
   GenericControllerClass1.A = 998;
    System.debug('###############'+ GenericControllerClass1.A);
}
public Pagereference CallMe(){
    Return page.P2;
}

}

My Second Controller is ………

public class TestControllerClass2 extends GenericControllerClass1{
    public TestControllerClass2(){
        System.debug('================='+GenericControllerClass1.A);
    }
}      

My First Page P1……

<apex:page controller="TestControllerClass1" >
<Apex:form >
    <apex:pageBlock title="Testing For Virtual Class">
        <apex:pageBlockSection title="Input Value in Virtual Class">
            <apex:commandButton value="Redirect" action="{!CallMe}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </Apex:form>
</apex:page>       

My Second page P2……

<apex:page controller="TestControllerClass2">
    <apex:pageBlock title="Testing For Virtual Class">
        <apex:pageBlockSection title="Output Value in Virtual Class">
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>    

My both Vf pages refering to the different Controller like … Controller1 & Controller2.Both the controller also refer to the same Main-Class. I'm using DebugLog to check the value of 'A' of second controller. But the value itself is showing 'null' there . I want to access the value of 'A' at Controller2, that(998) I have inserted in my
controller1… Where both the controller extends the Main controller variable. But in all cases value of 'A' in 'Main Class' & Second-Controller are showing null.. So how to get this value in any controller.. ???

Best Answer

Sounds like you are looking more for a static, persisted variable. Every time Apex code runs is done in a transaction:

An Apex transaction represents a set of operations that are executed as a single unit. All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, a Visualforce page, or a custom Web service method.

Unfortunately, to persist a value like the way you want to do it between controllers, you need to store that variable. I suggest using a Custom Setting:

Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, Apex, and the SOAP API.