2

I just created my own package(MyPackage) and class(MyClass) in pharo, using the System Browser. This is how it looks.

Object subclass: #MyClass
    instanceVariableNames: ''
    classVariableNames: ''
    category: 'MyPackage'

I understand the terms subclass and category, not getting the meaning of the other two terms (instanceVariableNames, classVariableNames).

  • 1
    Are you familiar with programming in any other OO language? Instance variables, class variables, and class instance variables are all differently scoped variables in object oriented programming, not just unique to Smalltalk. If you Google these terms, you'll find a variety of descriptions explaining the difference. – lurker Aug 29 '15 at 12:36
  • 1
    @lurker it is special. If you google for a "class variable" you will get an explanation of class instance variable. Also having class variables on the instance side and instance variables on the class side is really confusing. I think that in some way it's important to have this explanation on SO – Uko Aug 29 '15 at 13:20
  • 1
    @Uko yes, certainly searching for "class variable" will also bring up "class instance variable", so one does need to read the terminology carefully when reading articles on the topic (*i.e.*, are they using the phrase *class variable* versus *class instance variable*). I do agree that *class instance variable* need better clarification, and suitable for an SO topic. – lurker Aug 29 '15 at 13:21
  • I agree with @lurker - the difference between instance variables and class variables is basic OO and has an answer [here](http://stackoverflow.com/a/8622059/983430), for example, or the OP could've shown some research effort and at least read the "class variable" and "instance variable" entries in Wikipedia. – Amos M. Carpenter Aug 29 '15 at 13:52

1 Answers1

6

I have an impression that this question was already asked and answered, but I was not able to find it, probably it was on a mailing list.

Instance variables are just the variables that are personal to an instance, so each instance of the class that you are defining will have it's own set of variables.

Instance variables of the class side. You can define that when you switch to the class side, then you will se a code like this:

MyClass class
  instanceVariableNames: ''

In Pharo (and Smalltalks in general) each class is an instance of a meta class. The variables can be accessed by class-side methods, and as there is only a single instance representing class object, there will be only one set of these variables of that instance.

Class variables are the variables defined by classVariableNames: '' on the instance side template. I like to call them "pool variables", but in fact if you define such variable, all the instances from the hierarchy will be able to access it. Let's say that you have a class A and it's subclass B. If you have a "class variable" in A, you can access the same variable from both instances of A and B. It's like having a global variable for a hierarchy. I recommend not to use this type of variables.

Uko
  • 13,134
  • 6
  • 58
  • 106