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

Access data from separate class instance

Is it possible to access data that has been loaded into an instance of a class once it has been instatiated, from outside the class? I have a class that pulls some data from the database, puts it into a wrapper class, sorts it, and stores into a map. From a separate class, I want to be able to access the data from the map using a key value that is an instance variable in said class. I imagine this is a programming methodology that isn't exclusive to Apex, but is something like this possible?


A static method won't work because obviously I can't access instance data, and so I would have to replicate the data polling, wrapping, sorting and storing behavior within it, which I want to avoid. I also tried extending the class that wants to access the data from the initial class, but that doesn't work because when an instance of the second class is created, the first class is also instantiated again, which would be redudant. 


Definitely would appreciate some insight into this. Let me know if this doesn't make sense and/or want to know the use case behind this.




Hmm... you are right this seems to be more of a OOPS question than apex per say..


I could not understand your problem completely. But, If typically if you already have a class which has capability to do query, sort etc. and would like to use its functionality in another class you simply would expose its methods as public and create an object in the other class where you would to use it and call it. Something like below...


* Class which contains all library 
* methods
public class LibClass {
	public map<String,String> mSomeMap  ;	
	private list<Contact> lContact;
	public LibClass() {		
		mSomeMap = new map<String,String> ();
		lContact = new list<Contact>();
	public void doQuery(){
		lContact = [select name from contact limit 10];
	public void doSort() {
	public void populateMap() {
		// Populate mSomeMap map

* Class which uses LibClas
public class LibUserClass {

	public LibUserClass() {
		LibClass lcObject = new LibClass();
		String getValue = lcObject.mSomeMap.get("<Key Value>");




As I said I could be mistaken in understanding your problem statement, in which case please share the same in greater detail.