Computer Science Grade 9 20 min

Class Methods and Static Methods: Methods Related to the Class

Learn the difference between class methods and static methods and how to use them.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Differentiate between instance methods, class methods, and static methods. Correctly use the `@classmethod` decorator to create a method that operates on the class itself. Correctly use the `@staticmethod` decorator to create a utility method within a class. Explain the purpose of the `cls` parameter in a class method. Identify scenarios where a class method is more appropriate than an instance method. Create a class method that acts as an alternative constructor or factory. Explain why static methods do not have access to class or instance state. Imagine a `Game` class. How could the class itself keep track of the total number of players across ALL games, without asking a single game object? 🤔 In this lesson, we'll explore two special types of m...
2

Key Concepts & Vocabulary

TermDefinitionExample Instance MethodA regular method that belongs to a specific object (an instance). It always takes `self` as its first argument, which refers to the object calling the method.In a `Dog` class, `my_dog.bark()` is an instance method. It uses `self` to know which specific dog is barking. Class MethodA method that belongs to the class itself, not a specific object. It is marked with a `@classmethod` decorator and takes `cls` as its first argument, which refers to the class.A `Car` class might have a class method `Car.get_total_cars_made()` to return a count of all cars ever created from the `Car` blueprint. Static MethodA method that is part of a class's namespace but doesn't operate on the instance or the class. It is marked with a `@staticmethod` decorator and...
3

Core Syntax & Patterns

Class Method Syntax @classmethod def method_name(cls, param1, param2): # Code that uses 'cls' Use this pattern when you need a method that works with the class itself. The `@classmethod` decorator is required, and the first parameter must be `cls` (by convention) to receive the class. Static Method Syntax @staticmethod def method_name(param1, param2): # Code that does not use 'cls' or 'self' Use this pattern for utility functions that are related to the class but do not need to access any class or instance data. The `@staticmethod` decorator is required, and there is no special first parameter like `self` or `cls`.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Easy
Which decorator must be placed directly above a method definition to identify it as a class method?
A.@staticmethod
B.@classmethod
C.@instancemethod
D.@class
Easy
By convention, what is the name of the first parameter passed to a class method, which refers to the class itself?
A.self
B.object
C.cls
D.class
Easy
Which decorator must be placed directly above a method definition to identify it as a class method?
A.@staticmethod
B.@classmethod
C.@instancemethod
D.@class

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming: Designing with Classes and Objects

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.