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

What is Instantiation

What happens during instantiation?
What are we trying to do by instantiating?

What is the difference
when I declare
List<Account> accntList = new List<Account>();
and when I declare 
List<Account> accntList

Can someone please explain what happens with instantiation??

Many Thanks!

nitesh gadkarinitesh gadkari
Hi Sachin,
By using thisList<Account> accntList = new List<Account>(); you are creating an instance and at the same time instializing it with its default contructors
for more info refer this link

and when you declare List<Account> accntList; you just have created an instance of the list 

I hope i have satisfied your query.


@ Nitesh,
Thanks for your reply.

I have a slightly different understanding from the link you provided.
User-added image

It says,
List<Account> accntList = new List<Account>();  ----   I'm creating an Instance.
List<account> accntList;                                              ----   I'm just declaring a list variable.

I want to know the use of creating an instance.
Because sometimes I get 'attempt to de-reference null object' error if I try to use the list variable without instantiating it.

Many thanks!

nitesh gadkarinitesh gadkari
Hi again,

see what i deduced is that whenever you are using a list it has elements stored using indexing so that when you query certain records records are fetched using indexes stored at heap location.That is why you have to create instance and that is why you get error when you don't do this.