function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gourav JainGourav Jain 

What is the difference between?

List<Account> newAccount = new List<Account>();
vs.
List<Account> newAccount;

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

To be clear, there is "declaration" and "allocation" (also called instantiation). The act of declaring a variable means you intend to use that symbol (a variable's name is commonly referred to as a symbol), while the act of allocation means that you wish to dedicate memory to a variable. These are two distinct processes that are not necessarily related to each other. In your original post, the former example illustrates declaration and allocation, while the latter is only a declaration. You may also have an allocation with no declaration, in which case the variable becomes an "anonymous" variable. Consider these examples:

 

account anAccount;

Here, I declar a variable "anAccount" that holds a specialized SObject-typed memory structure. No memory is allocated, so I can not use it as a normal Account record yet (in other words, anAccount.Name would be an error).

 

account anAccount = [select id,name from account limit 1];

Here, I am simultaneously declaring a variable "anAccount", and also assigning a value from a query. If no such rows exist, this will cause a "no rows for assignment" exception.

 

account anAccount = new Account(Name='Aloha');

Now, I've declared a variable, and created an in-memory SObject. There is not necessarly any other account in the database with the name 'Aloha' at this point, as no "DML" (Data Manipulation Language) functions have been called. Once I "insert" or "upsert" this variable, a new account will be created. It would be an error in this case to try and "update" or "delete" the record, since it does not exist in the database at this point.

 

insert new Account(Name='Aloha');

This is a tricky example, but it shows that declaration isn't required for instantatiation. In this case, a new account is created from an anonymous variable. Once that line of code has passed, I won't have any way of knowing what the ID for that new account was. However, assuming I don't care about knowing the account, just knowing that it was created would be sufficient. I can use this to avoid storing a value in memory longer than I need to.

 

Similarly, it should be noted that allocation can happen without the "new" word through syntactic sugar. For example, a construct I've rarely seen used in code is the following:

 

List<String> helloWorld = 'Hello;World'.split(';');

Which skips a String declaration entirely; the string itself is instantatiated and then String.split(String) is called against an anonymous variable. Similarly, you can create a relatively compact if-then statement in some cases:

 

Set<String> guestList = new Set<String>{'Bob','James','Sue','Mary'};

String visitorName = 'Charles';

 

if(guestList.contains(visitorName)) {

  // this won't execute, because Charles isn't on the guest list.

}

You can even shorten this further by not naming guestList:

 

String visitorName = 'Charles';

 

if((new Set<String>{'Bob','James','Sue','Mary'}).contains(visitorName)) {

  // this won't execute, because Charles isn't on the guest list.

}

Anonymous variables are often glossed over in the Apex Code literature, but any well-trained developer uses them every day without thinking about them:

 

for(Account a:[SELECT Id,Name FROM Account]) ...

Where is the List<Account> that is created by invoking ths query? Of course, it's anonymous.

 

As one final note, you might simply have a declaration with no allocation simply because you intend to allocate this elsewhere. This might be especially common for member variables that might be allocated through different functions depending on how they are used. For example, this is very common in Apex Code:

 

public class A {

    public integer B;

 

    // Default Constructor

    public A() {

         B = 0;

    }

 

    // Parameterized Constructor

    public A(integer C) {

         B = C;

    }

}

 Anyways, I hope this helps clear up any confusion you might have had on declaration and allocation.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.