PDA

View Full Version : To Steve Kochan...a request




mdeh
Jan 21, 2009, 10:44 PM
I am enjoying your book immensely. But I have one small nit to pick.
Every now and again, you pose questions in the body of the book ( not the exercises) which may not be answered by the text of the book itself. For example. Page 211. A question to ascertain why class methods cannot access instance variables. A quick glance at the index, does not readily answer this. This is very frustrating. I would suggest, humbly, that you keep these questions for your exercises, or put aside some space at the end of each chapter to answer these. Now I **think** I know why, but this is a very fundamental aspect of understanding how different methods work, so it's nice to know if one is correct.
:)



caveman_uk
Jan 22, 2009, 02:53 AM
It might be better to email Kochan than post here as he may not read this.

Anyway, the answer to the question is because instance variables don't exist until the class is instantiated. A class method can be called without an instance of the class existing so therefore the variables might not exist when the class method is called.

mdeh
Jan 22, 2009, 07:52 AM
Anyway, the answer to the question is because instance variables don't exist until the class is instantiated. A class method can be called without an instance of the class existing so therefore the variables might not exist when the class method is called.

Well...let me play the devil's advocate. One of the things in Objective C is that it is permissible to send messages to nil objects. So, the fact that an instance may not exist is not really an issue? I think there is a deeper philosophical reason why one cannot do this. For instance, why not simply have a method like:

+(int/char/whatever) getIvar (Foo(i) *) f;

where Foo is the object one wishes to interrogate and (i) is the inth instance, so one could get a list of ivars with "for", or "while" statements ?

lee1210
Jan 22, 2009, 08:48 AM
Well...let me play the devil's advocate. One of the things in Objective C is that it is permissible to send messages to nil objects. So, the fact that an instance may not exist is not really an issue? I think there is a deeper philosophical reason why one cannot do this. For instance, why not simply have a method like:

+(int/char/whatever) getIvar (Foo(i) *) f;

where Foo is the object one wishes to interrogate and (i) is the inth instance, so one could get a list of ivars with "for", or "while" statements ?

Due to the ambiguity of English, you have chosen one interpretation, but probably not the one the author intended. The question, as I read it, is asking why a class method cannot access instance variables of that class like instance methods. The answer is that instance methods belong to each instance of the class, and a class method is not called on an instance. You are taking perhaps a more literal interpretation. There is no reason that a class method cannot access @public instance variables, or access instance variables through an accessor if the Object in question is passed in as an argument or is global. But, again, I don't believe that's the question that is intended. If class Foo has an instance variable Bar, and you call a class method on Foo, you won't have a Bar to reference.

-Lee

mdeh
Jan 22, 2009, 10:47 AM
Due to the ambiguity of English, you have chosen one interpretation, but probably not the one the author intended.
Exactly...which was the gist of my earlier note. I **think** I know what the idea of the question was, but it **could** be interpreted as a question which gets to the philosophy of ivars and access. The fact that on the previous page, there was an explanation of private vs protected vs public led me to believe that the question was more of a philosophical one.

The question, as I read it, is asking why a class method cannot access instance variables of that class like instance methods. The answer is that instance methods belong to each instance of the class, and a class method is not called on an instance.

Yes...I have to agree with you. I think the intention of the question was far more practical than I perceived.


There is no reason that a class method cannot access @public instance variables, or access instance variables through an accessor if the Object in question is passed in as an argument or is global. But, again, I don't believe that's the question that is intended. If class Foo has an instance variable Bar, and you call a class method on Foo, you won't have a Bar to reference.


Thanks Lee...you have put it into a little more perspective. I think the other issue is that the goal of OOP is encapsulation, and allowing class methods to access instance methods willy-nilly, would violate this principle?

MacRohde
Jan 22, 2009, 12:59 PM
It has nothing to do with breaking encapsulation. Although that would be the case if class methods could access instance methods or instance variables.

Think about this:

Let us say you have four instances of a class: obj1, obj2, obj3, and obj4.
If class methods were allowed to, or could, access instance variables (or methods), what instance variables should it access? A class method is coupled to a class not an instance, so should it be obj1's instance variables? Or obj2's? What about obj3's?

A class method has nothing to do with objects, therefore it makes no sense for it to be able access instance variables. Not only because it would break encapsulation, but because it raises the practical issues of what instance's instance variables should it access.