instance variables vs class variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

instance variables vs class variables

What is the difference between instance variable and class variable?

19th Feb 2017, 11:27 AM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
3 Answers
+ 2
Instance variables belong to theinstance of a class, thus an object. And every instance of that class has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class. public class Product { public int Barcode; }
19th Feb 2017, 1:37 PM
Xerox
Xerox - avatar
+ 1
Class variables there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.
19th Feb 2017, 1:38 PM
Xerox
Xerox - avatar
+ 1
Let me give you an example consider your smartphone's model as class , its specifications you read on some website is like the class's variables. Now, your smartphone's specs are as instance variables, so everything you change in your smartphone like background or apps in it etc is related to your instance of the smartphone's model
22nd Feb 2017, 2:38 PM
Rami Alsheikh
Rami Alsheikh - avatar