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
Mathew Andresen 5Mathew Andresen 5 

Understanding scope

Hi,

If I decalre a variable in the first part of my class outside any of the methods, it should be available within all of my methods yes?  But for some reason that's not working here, I don't understand what's wrong.  There must be somethign I'm missing

Thanks,
public class ContactReclassTest {
    
    String confirstName;
    String conlastName;
    List<Contact> conList = New List<Contact>();  // this should be seen by within all methods yes?
    
    ContactReclassTest() {
        conFirstName = 'Mathew';
        conlastName = 'Andresen';
        
    }
   
    
    public static void createContacts() {
        List<Contact> conList = New List<Contact>();  // If I don't put this in it says the variable doesn't exist
    for(Integer i = 0; i < 5; i++) {
        Contact con = new Contact(FirstName = 'Mathew', LastName = ('Andresen'+ i) );  //can't find the other strings I declared either
        conList.add(con);
    }
        insert(conList);
    
    }
}



 
Best Answer chosen by Mathew Andresen 5
AshwaniAshwani
You are trying to reference the member variable from a static method. This can not be done. You have to make variable static as well to access it.

All Answers

AshwaniAshwani
You are trying to reference the member variable from a static method. This can not be done. You have to make variable static as well to access it.
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
That's it.  Thanks so much!!! I couldn't understand why sometimes it seemed to work and other times it didn't.