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 Answer 1

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.

1
  • Thank you Ratan for the detailed response..thanks very much.
    – Yusuf Syed
    Commented Mar 15, 2016 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .