0
public class listexample {

    public list<string> myval {get;set;}
    public string str;
          public listexample(){
            myval=new list<string>();
            myval.add('India');
            myval.add('USA');
            myval.add('Japan');
            myval.add(1,'China');
                    }
   str =  myval.get(2);
                }
1
str =  myval.get(2);

Should be inside constructor

public class listexample {

    public list<string> myval {get;set;}
    public string str    {get; private set;}

    public listexample(){
            myval=new list<string>();
            myval.add('India');
            myval.add('USA');
            myval.add('Japan');
            myval.add(1,'China');
            str =  myval.get(2);
     }
}

Also since you are using str in VF page, should be in get set property

 public string str {get;set;}

If you are just displaying str in VF page. Use private set; to ensure this String should not updated from vf page.

  • Thank you Ratan for the detailed response..thanks very much. – Yusuf Syed Mar 15 '16 at 7:55

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.