Welcome to my blog

I have been working with Salesforce for quite a while, so don’t hesitate to contact me if you have any questions or want some advice.

s f

Subscribe
Follow Us
h

DIFFERENCE BETWEEN VARIABLE AND CONSTANT IN SALESFORCE APEX

 . Apex  . DIFFERENCE BETWEEN VARIABLE AND CONSTANT IN SALESFORCE APEX

DIFFERENCE BETWEEN VARIABLE AND CONSTANT IN SALESFORCE APEX

Learn Difference Between Variables & Constant in Salesforce Apex

Variable :-

Variable value can be change any time. it cant be fix.

How to declaring Variables :-

You can declare the variables in Apex like String and Integer as follows −

String strName = 'My String';  //String variable declaration
Integer myInteger = 1;         //Integer variable declaration
Boolean mtBoolean = true;      //Boolean variable declaration

Apex variables are Case-Insensitive :-

This means that the code given below will throw an error since the variable ‘a’ has been declared two times and both will be treated as the same.

Integer a = 100;
for (Integer i = 0; i<10; i++) {
   integer a = 1; //This statement will throw an error as a is being declared
   again
   System.debug('This code will throw error');
}

Constant :-

Constant value can not be change. As in any other programming language, Constants are the variables which do not change their value once declared or assigned a value.

Constants can be defined using the final keyword, which means that the variable can be assigned at most once, either in the declaration itself, or with a static initializer method if the constant is defined in a class.

static final Integer fstNumber=10;
(OR)
static final Integer fstNumber;
static{
  fstNumber=10;
}

Comments

  • zoritoler imol

    July 3, 2023

    Admiring the dedication you put into your blog and in depth information you offer. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Wonderful read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

    reply

Post a Comment