You need to sign in to do that
Don't have an account?
_why the unlucky stiff
What is the difference between?
List<Account> newAccount = new List<Account>();
vs.
List<Account> newAccount;
When would you declare and for what use?
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
List<Account> newAccount = new List<Account>();
vs.
List<Account> newAccount;
When would you declare and for what use?
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:
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).
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.
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.
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:
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:
You can even shorten this further by not naming guestList:
Anonymous variables are often glossed over in the Apex Code literature, but any well-trained developer uses them every day without thinking about them:
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:
Anyways, I hope this helps clear up any confusion you might have had on declaration and allocation.
All Answers
List<Account> newAccount = new List<Account>();
The above line will declare the newAccount and will create an account with blank value
List<Account> newAccount;
The above line will declare the valueable and it will have null value.
Just to validate and a little bit more idea. run this in Dev Console:
List<Account> newAccount = new List<Account>();
System.Debug('@1'+newAccount );
List<Account> newAccount1;
System.Debug('@2'+newAccount1 );
Thanks for the nice illustration! Can you ellaborate ona use case when you would use null declaration over the new List<Account>(); invocation?
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:
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).
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.
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.
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:
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:
You can even shorten this further by not naming guestList:
Anonymous variables are often glossed over in the Apex Code literature, but any well-trained developer uses them every day without thinking about them:
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:
Anyways, I hope this helps clear up any confusion you might have had on declaration and allocation.