+ Start a Discussion
Tyler HarrisTyler Harris 

Variable Does Not Exists

Why am I getting a Variable does not exist error for line 13: m4a[x].counter = counter + 1;
 
public class Mix4 {

    integer counter = 0;
    
    public static void main(){
        integer count = 0;
        
        Mix4 [] m4a = new Mix4[20];
        integer x = 0;
        
        while(x < 9){
            m4a[x] = new Mix4();
            m4a[x].counter = counter + 1;
            count = count + 1;
            count = count + m4a[x].maybeNew(x);
            x = x+1;
            
        }
        system.debug(count + ' ' + m4a[1].counter);
        
    }   
    
    public integer maybeNew(integer index){
        
        if(index < 1){
            Mix4 m4 = new Mix4();
            m4.counter = m4.counter + 1;
            return 1;
        }
        return 0;
    }
    
}

 
Best Answer chosen by Tyler Harris
David ZhuDavid Zhu
Main is a static meothod. It belongs to the class.
Counter is a instance property. same as maybeNew method. 
You cannot call instance proerpty or method from class metho.

I would suggest you remove "Static" keyword from Main method.

All Answers

David ZhuDavid Zhu
Main is a static meothod. It belongs to the class.
Counter is a instance property. same as maybeNew method. 
You cannot call instance proerpty or method from class metho.

I would suggest you remove "Static" keyword from Main method.
This was selected as the best answer
Tyler HarrisTyler Harris
Doh! Thanks David.