When working with list of sObjects returned from a SOQL query which of the below approach is better and please explain why

List<Account> accList1 = [Select Id From Account Where Name = 'Acme'];
List<Account> accList2 = new List<Account>([Select Id From Account Where Name = 'Acme']);

1 Answer 1


Whilst both achieve the same outcome I would always use the first of your two options.

The SOQL query naturally returns a list of sObjects, this is why you can use the syntax in the first line. This doesn't change when you execute your second line of code. Which means the SOQL returns a list and then you create a new list based on the list returned from the SOQL. This means that the platform is having to create a new reference to a list and then execute the addAll method. As can be seen in this snippet:

Debug snippet for second line of code

So, even though a debug log will count both statements as one line of Apex in reality the second is executing much more code. And as such I would always chose the syntax in your first line - keep it simple and make the most out of the platform features.

  • Nice answer! Welcome to StackExchange Mr Goodyear! ;-) Sep 19, 2013 at 7:59

You must log in to answer this question.

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