Class Methods
Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language:
- In some languages (e.g. C++, Java), class methods are synonymous with static methods (see section below), which are called with a known class name at compile-time.
this
cannot be used in static methods. - In some other languages (e.g. Smalltalk, Ruby, Objective-C), class methods are methods that are called on a class object, which can be computed at runtime, there being no difference between calling a method on a regular object or a class object; thus both instance and class methods are resolved dynamically, and there are no "static" methods. Notably, in these class methods,
this
refers to the class object. - Some languages have both. For example, in Python, one can create class methods and static methods using the
classmethod
andstaticmethod
decorators, respectively. The former has access tothis
(i.e. the instance object, conventionally known asself
), while the latter does not.
Read more about this topic: Method (computer Programming)
Famous quotes containing the words class and/or methods:
“I am both a public and a private school boy myself, having always changed schools just as the class in English in the new school was taking up Silas Marner, with the result that it was the only book in the English language that I knew until I was eighteenbut, boy, did I know Silas Marner!”
—Robert Benchley (18891945)
“A woman might claim to retain some of the childs faculties, although very limited and defused, simply because she has not been encouraged to learn methods of thought and develop a disciplined mind. As long as education remains largely induction ignorance will retain these advantages over learning and it is time that women impudently put them to work.”
—Germaine Greer (b. 1939)