Re: Obj-C, class methods and instance methods
Re: Obj-C, class methods and instance methods
- Subject: Re: Obj-C, class methods and instance methods
- From: Chris Hanson <email@hidden>
- Date: Sun, 19 May 2002 15:23:32 -0500
At 1:12 PM -0400 5/19/02, Sanj W. wrote:
Im trying to understand the difference between class methods and instance
methods in Obj-C. I thought that class methods are similar to static methods
in Java, where one could call the method without instantiating an class.
Semantics time: You don't call methods, the runtime invokes methods
when you send messages. This distinction is subtle but very, very
important to understanding the gestalt of real object-oriented
programming. (I wish people here would make it more often, it helps
to keep things clear.)
So a class method is invoked when you send a message to a class.
e.g. in the message expression [NSString stringWithString:@"foo"],
you're sending the stringWithString: message to NSString, which is a
class. The runtime will invoke NSString's stringWithString: class
method to handle this message.
However, I could write the following class without a constructor (init)
Objective-C doesn't have constructors. If your class doesn't respond
to -init, its superclass gets the chance to, and if its superclass
doesn't, *its* superclass does, and so on.
and
have an instance method (an IBAction) and get the system to call it (by
merely connecting in IB).
You can't wire a class as a target in Interface Builder, only an
instance. So you created an instance of your class, and the
Application Kit is sending -myAction: to your instance.
* Is there a default constructor in in the following class that I declared?
There are no constructors in Objective-C.
-- Chris
--
Chris Hanson | Email: email@hidden
bDistributed.com, Inc. | Phone: +1-847-372-3955
Making Business Distributed | Fax: +1-847-589-3738
http://bdistributed.com/ | Personal Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.