function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gopi biswalgopi biswal 

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. 
Best Answer chosen by gopi biswal
JeeTJeeT
Hi Gopi,
You can use inheritance with Apex Classes. refer an example below
public with sharing class ChildA extends ChildB {
  // varA, varB, varC will access same values from parent class; also can be used on extended visual-force page even
  
  public ChildA(ApexPages.StandardController ChildAController) {
    super(ChildAController);
  }

}

abstract public with sharing class ChildB extends Parent {
 //  varA, varB, varC will access same values from parent class; also can be used on extended visual-force page even

  public ChildB(ApexPages.StandardController ChildBController) {
    super(ChildBController); 
  }
  /*---- If required you overrides parent Class variables which will reflect to others----*/  
  protected override boolean isEmptyData(String myString){
    return String.isEmpty(getPhone(getObject(id)));
  } 
  
}

abstract public with sharing class Parent { 
	
	public String varA;
	public String varB;
	public String varC;
	
	abstract protected boolean isEmptyData(String customString);
	
	protected ApexPages.StandardController parentController;
	
	public Parent(ApexPages.StandardController childController){
		this.parentController = childController;
	}
}

OR else
you can declare static variable on an object with public access specifier and access those on other classes, So same value will be reflected on refered classes.

 

All Answers

Sagar PareekSagar Pareek
Do there are any dependencies between these classes? If no you cannot.
gopi biswalgopi biswal
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 TestControllerClass1 TC{get;set;}
    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 refer to the different Controller ... both the controller also refer to one Main-Class. I'm using DebugLog 
to check the value of second controller. But the value itself is showing 'null' there .I want to access the same value at Controller2 which I have inserted in my controller1... Where both the controller extends the Main controller variable.
JeeTJeeT
Hi Gopi,
You can use inheritance with Apex Classes. refer an example below
public with sharing class ChildA extends ChildB {
  // varA, varB, varC will access same values from parent class; also can be used on extended visual-force page even
  
  public ChildA(ApexPages.StandardController ChildAController) {
    super(ChildAController);
  }

}

abstract public with sharing class ChildB extends Parent {
 //  varA, varB, varC will access same values from parent class; also can be used on extended visual-force page even

  public ChildB(ApexPages.StandardController ChildBController) {
    super(ChildBController); 
  }
  /*---- If required you overrides parent Class variables which will reflect to others----*/  
  protected override boolean isEmptyData(String myString){
    return String.isEmpty(getPhone(getObject(id)));
  } 
  
}

abstract public with sharing class Parent { 
	
	public String varA;
	public String varB;
	public String varC;
	
	abstract protected boolean isEmptyData(String customString);
	
	protected ApexPages.StandardController parentController;
	
	public Parent(ApexPages.StandardController childController){
		this.parentController = childController;
	}
}

OR else
you can declare static variable on an object with public access specifier and access those on other classes, So same value will be reflected on refered classes.

 
This was selected as the best answer