Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bousozoku

Moderator emeritus
Jun 25, 2002
15,867
2,060
Lard
I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.
 

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
bousozoku said:
I've dabbled in it but I'm not proficient. It's a good thing to learn prior to learning Objective-C.


unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,867
2,060
Lard
scan said:
unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method

Instance methods are specific to the created (instance of the class) object but class methods are universal across created objects/instances.

This same concept works in SmallTalk, Objective-C, and Java, as well as some less pure object-oriented languages.
 

Jordan72

macrumors member
Nov 23, 2005
88
0
scan said:
unfortunately I need to learn smalltalk. one of my main questions is what is the difference between an instance method and a class method

There are two fundamental object types in Objective-C: Class objects and Instances.

When your code is exectued, the Objective-C runtime creates class object's. Class objects create objects, which are referred to as instances.

A class method is only used by the class object. An instance method is used by the instance created by the class object. (A class method's primary use is to create instances of a class.)

Here is an Objective-C message, where a class object is the receiving object:

Code:
[NSObject alloc];

Here is an Objective-C message, where an instance is the receiving object:

Code:
[myInstance alterData];

When you declare (as well as implement) a class method, you must put an addition sign in front of it.

Code:
+alloc;

When you declare a instance method, you must put a subtraction sign in front of it.

Code:
-alterData;


Objects needs to exist, before a message will work. When a program executes, if no object existed, such as the class objects, there would be no way to send messages to create objects. Class objects initiate the ability to send messages and class methods intiate the ability to create instance objects, which then allows messages to be sent between objects created in the program.
 

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
yeah i'm using squeak. i've never had this much trouble with a programming language.

I'm still not quite sure about the class and instance metods. So if you create an object of some class, then it can only use the instance methods? so how would the class methods be used? i'm unclear about that
 

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
pardon my ignorance, but could someone give me an example of how instance and class methods are used and when to use them? or perhaps comparing it with Java?

I have another question about smalltalk. How do loops work? I know there is no loops in smalltalk but there is a do method I believe? i'm not sure how the syntax goes.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
scan said:
pardon my ignorance, but could someone give me an example of how instance and class methods are used and when to use them? or perhaps comparing it with Java?

I have another question about smalltalk. How do loops work? I know there is no loops in smalltalk but there is a do method I believe? i'm not sure how the syntax goes.

Smalltalk class methods are equivalent to static methods in Java.
Smalltalk instance (or object) methods are equivalent to object methods in Java.

Loops in Smalltalk are handled by passing a block which contains the instructions for the body of the loop as an argument to the do: method.

For example:
Code:
// Java loop
for(int x=1; x<=20; x++) {
    System.out.println("" + x);
}
is written in Smalltalk by:
Code:
1 to: 20 do: [:x | x printNl ] !
 

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.

mrichmon said:
Smalltalk class methods are equivalent to static methods in Java.
Smalltalk instance (or object) methods are equivalent to object methods in Java.

Loops in Smalltalk are handled by passing a block which contains the instructions for the body of the loop as an argument to the do: method.

For example:
Code:
// Java loop
for(int x=1; x<=20; x++) {
    System.out.println("" + x);
}
is written in Smalltalk by:
Code:
1 to: 20 do: [:x | x printNl ] !
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
scan said:
yeah i'm using squeak. i've never had this much trouble with a programming language.

The thing about Smalltalk is that you absolutely must understand object-oriented programming. Most other less pure OO languages (such as C++, Java, C#, to some extent Objective-C) you can get away with only vague ideas of OO design.

For example, sub-typing is not the same as inheritence. Polymorphism does not necessarily require class inheritence. In Java and C++ it is easy to consider these three concepts to be the same thing. In fact, there are important differences.

The best way to get the concepts straight is to read the first chapter or two of a decent Smalltalk book and really take time to understand the information. Many of the Smalltalk books are structured in the same way using the same examples to illustrate concepts in the early chapters. This is because many Smalltalk books follow the examples given in the Smalltalk blue book. So most any book will do. If pressed I would recommend "Smalltalk: An Introduction to Application Development Using VisualWorks" by Hopkins and Horan, or "Smalltalk-80: The Language" by Goldberg and Robson (otherwise known as the Purple book... the Purple book is a reprint of the first 3 parts from the Blue book).
 

scan

macrumors 6502
Original poster
Oct 24, 2005
344
0
the book for my couse is SmallTalk, Objects, and Design by Chamond Liu. and i have read teh book very throough. with the problem I'm having, it literally talks about for 2 lines

mrichmon said:
The thing about Smalltalk is that you absolutely must understand object-oriented programming. Most other less pure OO languages (such as C++, Java, C#, to some extent Objective-C) you can get away with only vague ideas of OO design.

For example, sub-typing is not the same as inheritence. Polymorphism does not necessarily require class inheritence. In Java and C++ it is easy to consider these three concepts to be the same thing. In fact, there are important differences.

The best way to get the concepts straight is to read the first chapter or two of a decent Smalltalk book and really take time to understand the information. Many of the Smalltalk books are structured in the same way using the same examples to illustrate concepts in the early chapters. This is because many Smalltalk books follow the examples given in the Smalltalk blue book. So most any book will do. If pressed I would recommend "Smalltalk: An Introduction to Application Development Using VisualWorks" by Hopkins and Horan, or "Smalltalk-80: The Language" by Goldberg and Robson (otherwise known as the Purple book... the Purple book is a reprint of the first 3 parts from the Blue book).
 

Jordan72

macrumors member
Nov 23, 2005
88
0
scan said:
yeah i'm using squeak. i've never had this much trouble with a programming language.

I'm still not quite sure about the class and instance metods. So if you create an object of some class, then it can only use the instance methods? so how would the class methods be used? i'm unclear about that

I'm not famililiar with smalltalk syntax, but I can answer theoretically.

Think of the pupose of instance methods like c functions. They primarily are meant to alter data you send the function or alter data that is local to the function.

Think of the purpose class methods as to primarily malloc objects. You could think of them as mallocing structs. But there is something special about these structs, they have methods identified with them.

So to call a method, you must use the object that declares that method.

(If you give me a link to an editor and compiler, I'll give you some examples with correct syntax. I'm interested in Smalltalk, the inspiritor of Objective-C.)
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Dunno about smalltalk specifically but in general terms an instance method is a method that is performed by an object that is an instance of a class. A class method is one that is performed by the class itself (you don't need to have an instance defined)
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
Instance methods are the methods you're probably already familiar with if you've done any C++ or Java programming; whereas in Java you might have

myString.length(); or myString.componentsSeparatedByString(",")

this would be in Smalltalk

myString length or myString componentsSeparatedByString:','

the main thing here is that you instantiate a class, (create a variable to hold and instance of that type; i think the syntax is |myString| in Smalltalk) and the instance of the class is an object that responds to messages that you send it, such as length, or componentsSeparatedByString:

But the classes themselves are also objects. When you send a message to a class object, it does not have any information about what instances of the class are like; it's like putting client code in with the class, located there usually for convenience with one big exception: memory allocation. Memory allocation is done by the class object, usually in the "new" classs method. So in order for myString to actually store anything, you have to do

myString := String new

before you try do send length or componentsSeparatedByString: to myString. An example of a convenience class method would be something like separateString: intoComponentsBy: (I don't think this actually exists), which would return an array. You might use it like this:

|myArray|
myArray := String separateString:'apple,bat,cat,dog,elephant' intoComponentsBy:','

and it would be implemented like this, assuming the arguments are named stringToBeSeparated and separatorString, repectively:

|tmpString anArray|
tmpString :=String new
tmpString setString:stringToBeSeparated
anArray := tmpString componentsSeparatedBy:separatorString
^anArray

Incidentally, AFAIK the closest thing that Java and C++ have to this is constructors, which isn't saying much (DISCLAIMER: I don't know Java very well yet, so it's possible it has something and I haven't found it yet). Objective-C has full support for class methods, but currently little to no support for class variables (say you wanted to track the number of instances of a class), though it did in the past.
 

Jordan72

macrumors member
Nov 23, 2005
88
0
GeeYouEye said:
Objective-C has full support for class methods, but currently little to no support for class variables (say you wanted to track the number of instances of a class), though it did in the past.

Hey, I haven't used the potential for a class object to contain variables, but there seems to be no limitations, Apple says it better than I can:

The Objective-C Programming Language said:
Some classes declare static variables and provide class methods to manage them. (Declaring a variable static in the same file as the class definition limits its scope to just the class—and to just the part of the class that’s implemented in the file. Unlike instance variables, static variables can’t be inherited by subclasses.)

Static variables help give the class object more functionality than just that of a “factory” producing instances; it can approach being a complete and versatile object in its own right. A class object can be used to coordinate the instances it creates, dispense instances from lists of objects already created, or manage other processes essential to the application. In the case when you need only one object of a particular class, you can put all the object’s state into static variables and use only class methods. This saves the step of allocating and initializing an instance.

Note:*It is also possible to use external variables that weren’t declared static, but the limited scope of static variables better serves the purpose of encapsulating data into separate objects.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
Jordan72 said:
Hey, I haven't used the potential for a class object to contain variables, but there seems to be no limitations, Apple says it better than I can:

Note that the text from Apple only applies to Objective-C. Many languages (including Smalltalk if I recall correctly) and certainly Java and C++ have sub-classes inheriting both class/static methods and class/static fields.
 

Jordan72

macrumors member
Nov 23, 2005
88
0
mrichmon said:
Note that the text from Apple only applies to Objective-C. Many languages (including Smalltalk if I recall correctly) and certainly Java and C++ have sub-classes inheriting both class/static methods and class/static fields.

Apparently Objective-C class objects have copies of the field variables they declare. Although, I haven't tried to see if they have superclass field. I also haven't called an instance method. I'll give it a try.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
scan said:
is there some general rule of thumb when to write class methods. I don't quite understnad why adn when they are used.

Yes, scope and purpose will play a huge role. Class (static in Java) methods can only see class variables, while instance methods will be able to "see" instance AND class variables.

Here - this will give you more than you need. You really need to learn OO fundamentals and then worry about implementation.

http://www.inf.ufsc.br/poo/smalltalk/ibm/tutorial/content.html
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.