The difference between Class and Instance Variables in Java

Описание к видео The difference between Class and Instance Variables in Java

LESSON NOTES: https://therevisionist.org/software-e... In this video I show the difference between class and instance variables for the Java Programming language. Basically, instance variables will differ in value for each object built by the class constructor. However a single class variable will provide the same value for all of the objects of the class.
---
My Gear (づ⌐■ ͜ʖ■)づ
✪ I need to buy this webcam: http://amzn.to/2fWi3b7
✪ I like this keyboard a lot: https://therevisionist.org/reviews/th...
✪ This is the mic that I use now: http://amzn.to/2gqS6AO
♥ I love My SSD, so fast!: https://therevisionist.org/reviews/pn...
✪ HDD vs SSD Reliability: https://therevisionist.org/reviews/ss...
✪ SSD vs HDD power consumption: https://therevisionist.org/reviews/ss...
Follow me ┴┬┴┤( ͡° ͜ʖ├┬┴┬
✪ My Subreddit:   / bio_hacking  
✪ Facebook: https://www.facebook.com/profile.php?...
✪ Twitter:   / raqib_zaman  
✪ Google+: https://plus.google.com/+RaqibZaman
---
What’s the difference between a class variable and an instance variable?


Knowing the terminology is important. Instance variables and class variables are both member variables. They are both member variables because they are both associated with a specific class. But, there are differences between instance variables and class variables.
Instance variables
Instance variables belong to an instance of a class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has it’s own copy of the instance variables. Here is what a declaration of an instance variable would look like:
Example of an instance variable:
class Taxes
{
int count;
/*...*/
}
Class variables – also known as static member variables
Class variables, however, only have one copy of the variable(s) shared with all instances of the class. It’s important to remember that class variables are also known as static member variables in C++, Java, and C#. Each object of the class does not have its own copy of a class variable. Instead, every object shares the one and only copy of that class variable – and any changes made to that copy are seen by all of the objects of that class. Here is what a class variable – or a static member variable – would look like in C++:
Example of a class variable:
class Taxes
{
static int count;
/*...*/
}
Difference between class and instance variables


Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.

Комментарии

Информация по комментариям в разработке