---
schema_version: "1"
id: 64951c364cf0f3e3ba19ffdffa5c2931be1c52f10ebca80dad647557e172f17a
title: Assignment Statements
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_assignment_statements.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_assignment_statements.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: ef4148916f358d4ea1da0c32325b1b92e3852452ba72df0486a3c8a9597ed8db
description: An assignment statement is any statement that places a value into a variable.
---
# Assignment Statements

An assignment statement is any statement that places a value into a variable.

An assignment statement generally takes one of two forms:

\[LValue\] = \[new\_value\_expression\];
\[LValue\] = \[\[inline\_soql\_query\]\];

In the forms above, \[LValue\] stands for any expression that can be placed on the left side of an assignment operator. These include:

- A simple variable. For example:
    
    Integer i = 1;
    Account a = new Account();
    Account\[\] accts = \[SELECT Id FROM Account WITH USER\_MODE\];
- A de-referenced list element. For example:
    
    ints\[0\] = 1;
    accts\[0\].Name = 'Acme';
- An sObject field reference that the context user has permission to edit. For example:
    
    Account a = new Account(Name = 'Acme', BillingCity = 'San Francisco'); 
    
    // IDs cannot be set prior to an insert call
    // a.Id = '00300000003T2PGAA0';   
    
    // Instead, insert the record. The system automatically assigns it an ID.
    insert as user a;
    
    // Fields also must be writable for the context user
    // a.CreatedDate = System.today();  This code is invalid because
    //                                  createdDate is read-only! 
    
    // Since the account a has been inserted, it is now possible to
    // create a new contact that is related to it
    Contact c = new Contact(LastName = 'Roth', Account = a);
    
    // Notice that you can write to the account name directly through the contact 
    c.Account.Name = 'salesforce.com';

Assignment is always done by reference. For example:

Account a = new Account();
Account b;
Account\[\] c = new Account\[\]{};
a.Name = 'Acme';
b = a;         
c.add(a);      

// These asserts should now be true. You can reference the data
// originally allocated to account a through account b and account list c.
Assert.areEqual('Acme', b.Name);          
Assert.areEqual('Acme', c\[0\].Name);   

Similarly, two lists can point at the same value in memory. For example:

Account\[\] a = new Account\[\]{new Account()};
Account\[\] b = a;
a\[0\].Name = 'Acme';
Assert.areEqua('Acme', b\[0\].Name);  

In addition to \=, other valid assignment operators include +=, \*=, /=, |=, &=, ++, and \--. See [Expression Operators](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm "Expressions can be joined to one another with operators to create compound expressions.").

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions.htm "Expressions and Operators")
- [Next →](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_rules_of_conversion.htm "Rules of Conversion")