+ Start a Discussion
adrisseladrissel 

Can't access variable in method

My question is of a specific and general nature.  First the specific aspect.  I have the following code in my controller:

public class ListVisableCategories {

    // INSTANTIATE CONTROLLER
    
    public ListVisableCategories(){}
    

    // SET VARIABLES
    
    public Set<String> setRootCat{get;set;}
    public String prefix = Site.getPathPrefix();
    
    
    // DESCRIBE VISABLE CATEGORIES
    
    public static List<String> describeVisibleCategories(Map<String, String> mLabelCat, Map<String, List<String>> mCatStructure, Map<String, String> mParentCategory){
    
        if (prefix == '/xyz') {
    
            Set<String> setRootCat = new Set<String>{'Buyer_Articles'};
        }
    
        else if (prefix == '/abc') {
    
            Set<String> setRootCat = new Set<String>{'All'};
        }

        List<String> listVisableCategories = new List<String>();
        
         ........
            
            // IF APPLICABLE, GATHER ALL CHILD CATEGORIES
            
            if(setRootCat.contains(dc.getName())){
            
                for(DataCategory dcChild : dc.getChildCategories()){ 
                
                    listVisableCategories.add(dcChild.getName());
                }
            }
            
            else{
    
                listVisableCategories.add(dc.getName());
            }
        }
    
        return listVisableCategories;
    }

}

Ignoring other aspects of the code that may be missing (I didn't paste the entire controller) why in the world is it giving me a Compile Error for both the "setRootCat" and "prefix" variables saying that they "don't exist"?  Both variables are declared public.  Shouldn't I be able to access them later in the controller?

The general aspect of my question is why Apex doesn't allow simple usage of a variable across various functions in a class.  PHP, Java, Javascript...they all allow this, but declaring and using variables in various Apex methods in a class seems very cumbersome.  Maybe I'm missing something simple?

Thanks ahead of time!
Best Answer chosen by adrissel
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi

Your method seems to be a static method.
In apex you can not access non static variables inside static methods.

Thanks,
N.J

All Answers

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi

Your method seems to be a static method.
In apex you can not access non static variables inside static methods.

Thanks,
N.J
This was selected as the best answer
adrisseladrissel
That worked.  Thanks!