---
schema_version: "1"
id: e953c16ef169d4bf571981a759329dc22026fa4558ad003b8b657b629c236683
title: apex:variable
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/components/pages-compref-variable.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-compref-variable.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: b0f9395c5babbb70fa2ddd056d7d137f7d0a836709f596287139abf098849ea8
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# apex:variable

A local variable that can be used as a replacement for a specified expression within the body of the component. Use `<apex:variable>` to reduce repetitive and verbose expressions within a page.

Use this component to get user input for a controller method that does not correspond to a field on an sObject. Only `<apex:inputField>` and `<apex:outputField>` can be used with sObject fields.

**Note:** `<apex:variable>` does not support reassignment inside of an iteration component, such as `<apex:dataTable>` or `<apex:repeat>`. The result of doing so, e.g., incrementing the `<apex:variable>` as a counter, is unsupported and undefined.

## Example

```apex
<!-- For this example to render properly, you must associate the Visualforce page 
with a valid contact record in the URL. 
For example, if 001D000000IRt53 is the contact ID, the resulting URL should be: 
https://MyDomain_login_URL/apex/myPage?id=001D000000IRt53
See the Visualforce Developer's Guide Quick Start Tutorial for more information. -->
         
<!-- Page: -->
<apex:page controller="variableCon">
    <apex:variable var="c" value="{!contact}" />
        
    <p>Greetings, {!c.LastName}.</p>
</apex:page>

/*** Controller ***/
public class variableCon {
    Contact contact;

    public Contact getContact() {
        if (contact == null){                      
        contact = [select LastName from Contact where 
            id = :ApexPages.currentPage().getParameters().get('id')];
        }
        return contact;
    }
}
```

## Attributes

|Attribute Name|Attribute Type|Description|Required?|API Version|Access|
|--------------|--------------|-----------|---------|-----------|------|
|`id`|String|An identifier that allows the component to be referenced by other components in the page.| |10.0|global|
|`rendered`|Boolean|A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true.| |10.0|global|
|`value`|Object|The expression that can be represented by the variable within the body of the variable component.|Yes|10.0|global|
|`var`|String|The name of the variable that can be used to represent the value expression within the body of the variable component.|Yes|10.0|global|

