Hello. In this course
we'll take the time to really
understand the class methods.
You should have done the exercise
with the counter,
you have done a class method,
it worked very well.
Now we'll take a good look,
so you understand for once and all.
You'll learn in this course
that there's no difference in Pharo
between class and instance methods
on a search algorithm level.
There's only one search algorithm
alluded to and used,
that's defined in the virtual machine,
and you'll see that, while we could
have said that the class methods,
at first glance, are like static
Java methods, they're not.
In fact, they are dynamically linked.
There's a search at execution,
while in Java, typically,
static methods aren't dynamically sought.
Let's see how it works.
In fact, what you've seen
is that there's only one lookup,
which starts in the receiver's class.
If the method is defined in the class,
it's returned, otherwise
it continues in the superclass.
What this means, graphically,
is that I've my example
of the Counter class.
When I send the increment message,
what will happen?
Step 1, I look in the Counter class,
and I'll go back up...
If I've "increment" it's defined here,
if it's a method defined
higher up in the hierarchy
it will be sought higher up.
In fact, this mechanism is used
in Pharo, there's only one,
it's used for both instances and classes.
When you did the exercise with
the class counter and sent the message
"with value" to the class counter
to create an instance, what happened?
The class counter is instance
of a class called "Counter class"
and we looked, we used
the same method as before.
We looked in the receiver class,
the receiver was "Counter",
so we looked in the class
"Counter class" and found the method
"with value" which we applied
to the receiver
and it gave us a new instance.
So we used exactly the same process
as for the messages we sent
to an instance of the Counter class.
So what is a "class" in Pharo?
A class is an instance.
It's an object like any object.
In Pharo there are only objects.
A class is an instance
of another class, called a metaclass.
It's just to distinguish them.
A metaclass is just a class
whose instances are classes,
to differentiate between
terminal instances and classes.
The next thing is that
the Counter class, in this line,
the Counter class is the only instance
of the class Counter class.
This Counter class is created
automatically, without you knowing it.
When you defined the Counter class,
the system automatically created
the class Counter class,
and it created the Counter class.
You thought you were creating 1 class,
but in fact you were creating 2.
What you have to know in Pharo
is that all the classes
in a class called XXX
are called XXX class.
So if I have Counter,
the Counter class will be
Counter class, systematically.
So, in fact, what you do in reality,
we're just showing you how it works.
We needn't have explained it,
but it means that when
the browser shows you
the Counter class, it shows you
the code that will be executed
on the instances of this class.
That means, "increment"
is the code that will be executed
on this instance here.
When you click on the "class" button
to define a class method,
it shows you the code that will be
executed on this object here.
And in fact, if we look,
when I send the message "increment"
to the counter instance,
I'll look in the Counter class.
Look, that's the method executed.
When I send the message
"with value" to the Counter class,
where will I look? In its class.
I follow this link,
and it's this method here.
So what should you know?
A class is an object in Pharo.
A class can receive messages.
I told you when we did the syntax,
I told you date, today...
What's that? It's a message
that we sent to a class.
There are no elements
syntaxed differently.
It's the same as sending a message
syntactically to an instance.
What's more, this course shows you,
there's only one method lookup.
The class methods
are looked up dynamically
like the instance methods.
Where are they looked up?
In the class of the receiver.
When it's an instance,
it's looked up in the class,
when it's an instance class
like the Counter class
it's looked up in the metaclass,
in the class called Counter class.
So a class is an instance
of another class, called a metaclass,
and only one look up works.
So we'll return to this notion
in the final session,
in the lecture
Understanding Metaclasses
and we're going to revisit,
we're going to lift the lid on Pharo
and show you how it works,
and it's completely uniform.