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
Newbie10Newbie10 

difference in sobject declaration

Can anyone please explain what is the difference between two code below

 

1.

Account acc1 = new account(name = 'a');

 

2.

 

Account acc1 ;

 acc1 = new account(name = 'a');

 

 

 

In developer console i could see ,variable assignment of acc1 with a memory location ='0X352e62d6' at end of code 1

and at end of code 2,variable assignment of acc1 is null

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
account a1 = new account(name='test');
account a2;
a2 = new account(name='test');

 

EXECUTION_STARTED
CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
VARIABLE_SCOPE_BEGIN|[1]|a1|Account|true|false
VARIABLE_SCOPE_BEGIN|[2]|a2|Account|true|false
HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
STATEMENT_EXECUTE|[1]
STATEMENT_EXECUTE|[1]
HEAP_ALLOCATE|[1]|Bytes:4
HEAP_ALLOCATE|[1]|Bytes:4
VARIABLE_ASSIGNMENT|[1]|this.Name|"test"|0x72f0067d
VARIABLE_ASSIGNMENT|[1]|a1|{"Name":"test"}|0x72f0067d
STATEMENT_EXECUTE|[2]
VARIABLE_ASSIGNMENT|[2]|a2|null|
STATEMENT_EXECUTE|[3]
HEAP_ALLOCATE|[3]|Bytes:4
VARIABLE_ASSIGNMENT|[3]|this.Name|"test"|0x6c3a337e
VARIABLE_ASSIGNMENT|[3]|a2|{"Name":"test"}|0x6c3a337e
CODE_UNIT_FINISHED|execute_anonymous_apex
EXECUTION_FINISHED

As you can see, there is a memory location assigned, as expected. The memory address at line 2 is NULL because it hasn't been assigned yet; the memory is allocated on line 3 when the new keyword is used.

All Answers

sfdcfoxsfdcfox
Your log levels might not be set to a fine enough level to see the memory address of acc1 in the second case. Regardless, the only difference between the two examples is that the former uses 1 script statement, while the latter uses two. In a loop of 20,000 records, the first method would use 20,000 less script statements than the latter (the limit for lines executed is 200,000).
Newbie10Newbie10

Thanks for clarifying.Even i feel they are the same.and for some reason value is not being displayed.

I did try changing loglevel set to finest for everything.and still giving me same result though :(

sfdcfoxsfdcfox
account a1 = new account(name='test');
account a2;
a2 = new account(name='test');

 

EXECUTION_STARTED
CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
VARIABLE_SCOPE_BEGIN|[1]|a1|Account|true|false
VARIABLE_SCOPE_BEGIN|[2]|a2|Account|true|false
HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
STATEMENT_EXECUTE|[1]
STATEMENT_EXECUTE|[1]
HEAP_ALLOCATE|[1]|Bytes:4
HEAP_ALLOCATE|[1]|Bytes:4
VARIABLE_ASSIGNMENT|[1]|this.Name|"test"|0x72f0067d
VARIABLE_ASSIGNMENT|[1]|a1|{"Name":"test"}|0x72f0067d
STATEMENT_EXECUTE|[2]
VARIABLE_ASSIGNMENT|[2]|a2|null|
STATEMENT_EXECUTE|[3]
HEAP_ALLOCATE|[3]|Bytes:4
VARIABLE_ASSIGNMENT|[3]|this.Name|"test"|0x6c3a337e
VARIABLE_ASSIGNMENT|[3]|a2|{"Name":"test"}|0x6c3a337e
CODE_UNIT_FINISHED|execute_anonymous_apex
EXECUTION_FINISHED

As you can see, there is a memory location assigned, as expected. The memory address at line 2 is NULL because it hasn't been assigned yet; the memory is allocated on line 3 when the new keyword is used.

This was selected as the best answer
Newbie10Newbie10

Yes i do agree there is a memory location assigned.

I am comparitively new to developer console  and i was wondering why variable value(on right pane in developer console)doesnt change to this memory location there.But remains 

null through out.

 

Otherwise yeah you are completely right and many thanks for looking into this

 

sfdcfoxsfdcfox
Must be a bug in the debug log parser.