Skip to main content

Main Code:  

public class NewConstEx { 

public list<string> subjects; 

    public NewConstEx(list<string> subjectlist){ 

        subjects=subjectlist; 

    } 

} 

 

anonymous Window:  

list<string> mysubject = new list<string>{ 

    'Apex', 

        'LWC', 

        'JS', 

        'Java' 

}; 

NewConstEx N = new NewConstEx(mysubject); 

system.debug(N.subjects); 

 

#Apex  #Salesforce Developer

2 respuestas
  1. 3 sept, 11:05

    @Himanshu Shekhar Yes, you can declare the List in the main Apex class itself.

    For example:

    public class NewConstEx {

    public List<String> subjects = new List<String>{

    'Apex',

    'LWC',

    'JS',

    'Java'

    };

    public NewConstEx() {

    }

    }

    Then Anonymous Window:

    NewConstEx N = new NewConstEx();

    System.debug(N.subjects);

    Your original approach is also correct:

    List<String> mysubject = new List<String>{'Apex', 'LWC', 'JS', 'Java'};

    NewConstEx N = new NewConstEx(mysubject);

    Constructor is useful when you want to pass different Lists/values from outside. If the List is always fixed, you can initialize it directly in the class. 

0/9000