Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 21, 2009, 10:44 PM   #1
mdeh
macrumors 6502
 
Join Date: Jan 2009
To Steve Kochan...a request

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.
mdeh is offline   0 Reply With Quote
Old Jan 22, 2009, 02:53 AM   #2
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
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.
caveman_uk is offline   0 Reply With Quote
Old Jan 22, 2009, 07:52 AM   #3
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by caveman_uk View Post

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:

Code:
+(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 ?
mdeh is offline   0 Reply With Quote
Old Jan 22, 2009, 08:48 AM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by mdeh View Post
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:

Code:
+(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
lee1210 is offline   0 Reply With Quote
Old Jan 22, 2009, 10:47 AM   #5
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
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.

Quote:
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.


Quote:
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?
mdeh is offline   0 Reply With Quote
Old Jan 22, 2009, 12:59 PM   #6
MacRohde
macrumors regular
 
Join Date: Jun 2004
Location: Copenhagen, Denmark
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.
__________________
My main systems: iMac 2.7 GHz (Mountain Lion), iPad 2, iPhone 4S.

Last edited by MacRohde; Jan 22, 2009 at 01:05 PM.
MacRohde is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Learning Obj C - Kochan or Big Nerd Ranch? afousek iPhone/iPad Programming 4 Apr 26, 2014 01:45 PM
Employees File Request to Include Steve Jobs Evidence in Anti-Poaching Lawsuit MacRumors Mac Blog Discussion 27 Apr 24, 2014 11:51 AM
Steve Wozniak and Atari Founder Nolan Bushnell Recall Steve Jobs and Early Apple Memories Together MacRumors Mac Blog Discussion 69 Sep 30, 2013 02:42 PM
Ringtone Request oktoa21 iPhone 4 Feb 6, 2013 05:41 PM
App Request secres77 iPhone and iPod touch Apps 3 Jan 16, 2013 09:24 PM

Forum Jump

All times are GMT -5. The time now is 08:53 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC