+ 2
instance variables vs class variables
What is the difference between instance variable and class variable?
3 Antworten
+ 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;
}
+ 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.
+ 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