Dart Instance Variable vs Reference Variable

Add Your Heading Text Here

Instance variables and reference variables are very useful concepts of object oriented programming structure to deeply understand the class and object concept.

So let’s get started with instance variable.

What is Instance Variable ?

Instance variable is a variable declared inside the class but outside any function(method) or any block of code.

Basically, instance variables are the properties of class. every object of a particular class has  individual copy of all instance variables.

Instance variables are created with object creation and gets destroyed with object destruction.

The scope of instance variables differs from object to object of particular class. Which means each copy of instance variables belongs to particular object of that class.

For ex :

class Avengers{
  int rank;
  String Name;
  Avengers(int avengerRank,String avengerName){
    rank=avengerRank;
    Name=avengerName;
    print("Rank of " + Name + " is " + rank.toString());
  }
}

void main() {
  Avengers marvel=new Avengers(1,"Captain Marvel");	
  Avengers thor=new Avengers(2,"Almighty Thor");
  Avengers hulk=new Avengers(3,"The Hulk");
}

Output :

Rank of Captain Marvel is 1
Rank of Almighty Thor is 2
Rank of The Hulk is 3

In above example, there are 3 objects Captain Marvel, Thor and Hulk of a particular class Avengers. Marvel, Thor and Hulk will get their separate copy of instance variables (i.e., Name and Rank) generated at the time of object creation. It is not required to create or declare properties for each object of class.

The default values for uninitialized variables of any data type in dart is null. Because everything else in dart is an object.

What is Reference Variable?

The reference variable reference to or point to the memory storage allocated for particular object created.

In simple language, reference variable is the alias or identifier for the declared objects.

For ex :

class Test{
}
void main(){
int x;	// x point to some random location like 0x02304 for object of type(class) int variable declared.

String y;	// y point to some random location like 0x089540 for object of type(class) String variable declared.

Test T1=new Test();	// test points to some random like 0x02560 for  object of type(class) Test declared.
}

In above example, x, y and T1 are different variables pointing to their respective objects stored in the memory.

They are just references of the objects to easily access them.

At a time, a reference variable can refer or point to the single object of the class. But, multiple reference variable can refer or point to same object.

For ex :

class Books{
}

void main(){
/* New Object of Class Book is created and referenced by the Reference variable B1.*/
	Book B1=new Book();	

/* Reference Variable B2 is created and ultimately refers to the same object referred by B1.*/
	Book B2=B1;	

/* New Object of Class Book is created and referenced by the Reference variable B3. */
	Book B3=new Book();	

/* Reference Variable B3 is created and ultimately refers to the same object referred by B2. */
	B3=B2;
}

In above example, the first statement will create a new object and will get some random storage location in memory. Let say, 0x0001 which is referenced by Reference variable B1.

The second statement will now have another reference variable B2 which will refer to the same object which is referenced by Reference variable B1 i.e., location 0x0001.

The third statement will create a new object and will get some random storage location in memory. Let say, 0x0003 which is referenced by Reference variable B3.

In 4th Statement reference variable B3 will now refer to the same object which is referenced by Reference variable B2 i.e., location 0x0001.

So now we have 2 Objects of class Book and 3 Reference Variables B1, B2 and B3 all pointing to the first object only. While, second object is not referenced by any reference variable.​

So this is the brief explanatory section for instance variable and reference variable.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lalit
Lalit
3 years ago

Hi, Thanks for writing this useful post. Question for you: In the Book example, at the end no one is referring to memory location : 0x0003, Right? 

Jay Patel
Admin
Jay Patel
3 years ago
Reply to  Lalit

Correct. It’s just an example for understanding.

I’m glad you liked this post.

Show Buttons
Hide Buttons