Can anyone tell me about the difference between class variables and class instance variables?
Can anyone tell me about the difference between class variables and class instance variables?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A class variable (
@@) is shared among the class and all of its descendants. A class instance variable (@) is not shared by the class’s descendants.Class variable (
@@)Let’s have a class Foo with a class variable
@@i, and accessors for reading and writing@@i:And a derived class:
We see that Foo and Bar have the same value for
@@i:And changing
@@iin one changes it in both:Class instance variable (
@)Let’s make a simple class with a class instance variable
@iand accessors for reading and writing@i:And a derived class:
We see that although Bar inherits the accessors for
@i, it does not inherit@iitself:We can set Bar’s
@iwithout affecting Foo’s@i: