Classes and objects
A class is a plan; objects are the things built from it. Follow one small Player class as it produces two objects, runs a method on one of them and keeps a count for itself.
In 30 seconds
A class is a template: it lists the data (fields) and the actions (methods) that a kind of thing will have. An object is one real thing built from that template, with its own values.
A special method, the constructor, runs when an object is created and fills in its starting values.
Each object keeps its own copy of the instance fields, so changing one object leaves the others alone. A static member is different: it belongs to the class itself and exists only once.
Classes and objects, step by step
The code is JavaScript. Java, C#, Python and others share these ideas with different spelling, and in some of them fields must be declared with a type.
- Step 1: A class is a template. A class describes a kind of thing without being one. This one says what every Player will have: two fields (pieces of data), name and score, and methods (things it can do), such as add. Writing the class creates no player at all. It is the plan, not the building.
- Step 2: An object is an instance. The word new builds an actual object from the template. That object is called an instance of the class. It lives in memory, its slots are filled with real values, and the variable ana refers to it. The class itself is unchanged and can be used again.
- Step 3: The constructor sets it up. When new runs, it first makes an empty object, then calls the class’s constructor, a special method for setting things up. Inside it, the word this means the object being built. The constructor copies the argument into this.name and starts this.score at 0, so no Player is ever left half-made.
- Step 4: Each object has its own data. Call new again and the same template produces a second, separate object. ana and ben have the same fields and the same methods, because they share a class, but each keeps its own values. Fields like these, with one copy per object, are called instance fields.
- Step 5: Methods act on one object. A method is written once, in the class, but it always runs on behalf of one particular object: the one before the dot. In ana.add(10), this is ana, so only ana’s score changes, from 0 to 10. ben’s score is still 0.
- Step 6: Static: one for the class. Some data belongs to the class as a whole, not to any one object. Marked static, count exists exactly once, on Player itself. The constructor adds 1 to it each time an object is made, so after two calls to new it is 2. You read it through the class: Player.count.
- Step 7: Instance vs static. Here is the whole program and where everything ended up. Instance members, name and score, exist once per object and are reached through an object. The static member, count, exists once for the class and is reached through the class. One class, any number of objects.
Try it yourself
Learn this first
Learn next
- PolymorphismOne method call, many behaviours: each object answers in its own way. Intermediate
- Prototypal inheritanceJavaScript objects inherit by linking to other objects and searching up the chain. Intermediate
- Garbage collectionThe runtime traces what a program can still reach and frees everything else. Intermediate
Spotted a mistake on this page?