1
public class WrapExample {
    public List<Wrap_Example1> wps=new List<Wrap_Example1>();

    public static void WrapExampleDemo(){

       Wrap_Example1 w=new Wrap_Example1();
        w.sName='manoj';
        w.age=24;
        w.salary=10000;
       wps.add(w);
        for(Wrap_Example1 e:wps){
            System.debug('candidate name is '+e.sName);
            System.debug('candidate age is '+e.age);
            System.debug('candidate salary is '+e.salary);
        }
    } 
    public class Wrap_Example1{
        public String sName;
        public Integer age;
        public Decimal salary;
    }

}
4

Variables declared outside a method are placed in either the static scope or instance scope of the class. The variable does not have the keyword static, and so therefore it is an instance variable. You cannot access an instance variable from inside a static method, as no object exists. You will need to either move the variable within the function (1), make the function non-static (2), or make the variable static (3).


// Option 1
public class WrapExample {
    public static void WrapExampleDemo(){
        List<Wrap_Example1> wps=new List<Wrap_Example1>();
// ...

// Option 2
public class WrapExample {
    public List<Wrap_Example1> wps=new List<Wrap_Example1>();
    public void WrapExampleDemo(){
// ...

// Option 3
public class WrapExample {
    public static List<Wrap_Example1> wps=new List<Wrap_Example1>();
    public static void WrapExampleDemo(){
// ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.