Difference between new and initialize in Smalltalk?
Exactly. When you send the message #new, it not only creates the object, but sends the messagge #initialize. This let you customize the initialization of objects. Look:
Behavior >> new
"Answer a new initialized instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable."
^ self basicNew initialize
And then:
ProtoObject >> initialize
"Subclasses should redefine this method to perform initializations on instance creation"
And:
Behaviour >> basicNew
"Primitive. Answer an instance of the receiver (which is a class) with no
indexable variables. Fail if the class is indexable. Essential. See Object
documentation whatIsAPrimitive."
<primitive: 70>
self isVariable ifTrue: [ ^ self basicNew: 0 ].
"space must be low"
OutOfMemory signal.
^ self basicNew "retry if user proceeds"
So...#basicNew is the primitive that creates the object. Normally, you use #new and if you don't want anything in special, you don't implement #initialize and hence the empty implementation of #ProtoObject will be executed. Otherwise, you can directly send #basicNew but you probably shouldn't do this.
Cheers
With new you create a new Object, while the initialize method is executed when the new Object is created, and initializes the Object.
Bavarious is correct.
Behavior >> new
"Answer a new instance of the receiver. If the instance can have indexable variables, it will have 0 indexable variables."
"Primitive fails if the class is indexable."
<primitive: 70>
self isVariable ifTrue: [^self new: 0].
self primitiveFailed
Object >> initialize
"Initialize the object to its default state. This is typically used in a convention that the class implements the #new method to call #initialize automatically. This is not done for all objects in VisualWorks, but the initialize method is provided here so that subclasses can call 'super initialize' and not get an exception."
^self.