What is an Apex variable in Salesforce and Types of Variables?

Hello everyone so in this post we will discuss about variables so what is a variable? Variable is the name of the memory location. In another way, it is a user-defined name which is given by the user. While running the program so we need to store some value in one place so that place is called the variable.

What is an Apex variable in Salesforce and Types of Variables

Apex Variable Types in Salesforce

There are 3 types of variables available in salesforce.

  • Local variable
  • Instance Variable
  • Non-static Variable

Local Variable

A Local variable is a variable that is declared inside the method or a constructor or method parameter called a local variable.

Instance Variable

You can declare that instance variables belong to an object because they are associated with a class instance. It is declared outside of any blocks, methods, or constructors. They have unique values for each and every class instance – this means you can use multiple instances of the same class variable.

Class Variable

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor, or a block.

Apex Variable Examples

Global class FirstApex_Class {

Integer i = 100; // Instance Variable
Public static boolean b1=true; //Static variable or class variable

Public static void m1(){
String s1='Salesforce variable'; //Local Variable


How to Declare Variable in Apex Class?

String s='Mystring'; // String variable declaration
Integer i=1;         // Integer variable declaration
Boolean b=true;      // Boolean variable declaration