Understanding Shared, Local, and Class Variables: Usage Explained



Delving into the intricate realm of local, class, and shared variables within a complex application can provide valuable insights for programmers. By carefully considering their usage and implications, developers can enhance the efficiency and effectiveness of their code. Remember, thorough analysis and planning are essential steps towards achieving successful outcomes in software development.

Getting Started:

Local Variables:

The variables which being declared in a specific block (method/function), and can be consumed within that place. You may refer this example, which is written for JavaScript. You can refer the syntax of your application language.
function ValidateEmail (email:string) {
let pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// Here I have defined a pattern variable which is under ValidateEmail method.
// And how It will be consumed within the method.

return pattern.test(email) ? true : false

function formData(Form:any) {
// Check here the email from the Form is valid or invalid. And also try to print the pattern variable defined in ValidateEmail, what is the result
if (this.ValidateEmail(Form.email)) {
console.log("Given form email is valid..");
} else {
console.log("Form email is invalid..");
console.log(this.pattern); // Will throw error, pattern is not defined. because pattern has only access under ValidateEmail.

In the above example you can see that the `pattern` variable is only accessible in the ValidateEmail(), and not from out side of it. If you try to access it in outside, it will throw error.
When to Use Local variables: 

When you require to store a temporary data, within a block of method, freely you can use this type of variables, because after the execution of the method block, it will be released from the memory instantly. 

Class Variables:

It's bit advanced type of local variables. These variables can be defined with an access specifier like
  • public
  • private
  • protected

It depends on the access specifier, at what places these variables will be accessible. I will  write a class to demonstrate the use of it.

export class test1{
public value1:string = "Hello";
private value2:string = "BitHost";
protected value3:string = "World"


return this.value1 + newValue;

return this.value1 + this.value2;

return this.value1 + this.value3;


export class test2 extends test1{
private randomVal:any;
console.log(this.get_complete_string_with_public("Ram Krishna"));// Print "Hello Ram Krishna"
console.log(this.value1 + this.value2);// Will throw error, variable not accessible from outside of class.

export class test3 {
let abc = new test1();

console.log(abc.value3); // will throw error, protected property not accessible.
console.log(abc.value2); // will throw error, private property not accessible.
console.log(abc.value1); // will print "Hello"

This is how the class variables works.

These above two examples are for simple programming or simple learnings. But when you go with the architectural point of view of a complex application, you come to know about the shared variables. Shared variables are also like public variables, but can be utilised throughout the application. 

It allow to perform transactional operation within the app, without loosing data. You may refer this context like, you have need a value every where in app, so it will be better to store that at common place from there, it can be consumed by other objects.

Taking an example in reference of user data we commonly have in all web/mobile applications like 
userData = {
name: "Test Alpha",
id: "DSY6325BGHD6236",
access_token: "jkfhufs87783b3hurbdsuayt@#$2567vuycsfsvnxhfgd",
refresh_token: "dhgshdhsdghgdgsdhsgdhgds!!!!!!!!!eeeeeeeeeee",
expires_in: 1345234,
isActivated: true,
isAdmin: false,
metaData: {
profile_pic: "https://bithost.in/logo.png",
address: "Some address"

This above data I need in every page of the application, so It's better to store at common place instead of getting it again and again.

Taking example of a product, in a e-commerce application, which need from the initial stage of viewing of product detail till the checkout of the bill. So It can be used as a shared one, where multiple page will require the same product to perform few operation on it.
This is a basic example, not the implemented thought.


Understanding Shared, Local, and Class Variables: Usage Explained
Ram Krishna August 28, 2021
Share this post
Our blogs
Sign in to leave a comment
Deploying Your Node.js App to Heroku from GitLab: A Comprehensive Guide