difference between method and ClassMethod of Class definition
What are the difference between method() and ClassMethod() in Cache Class definition
Comments
Caché Programming Orientation Guide explains this in details. Please read: http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GORIENT_ch_classprog#GORIENT_classprog_method
Method() is at the object level (the instance of the class) and ClassMethod() is at the class level .
Lets say that we have a Car class then:
Method PrintCar() {
//print the car number
}
ClassMethod PrintAllCars() {
//for each car call it's PrintCar()
}
As a consequence of the previous answers it is not allowed to address instance properties using the "dot dot" syntax within a class method. Using the previous example: it is ok to use
set tCarNumber=..CarNumber
within the PrintCar() method (providing there is a CarNumber property defined)
but you cannot use it in ClassMethod - as a class method can be called outside of any instance context.
method() is an instance method; you need a car object in order to call it.
set car = ##class(Car).%New()
d car.PrintCar()
ClassMethod() you dont' need an object (provided it's not private).
d ##class(Car).PrintAllCars()
If you know Java, ClassMethod is like a static method; method is like an instance method.
The doc. has an section on this:
" There are two kinds of methods in a class language: instance methods and class methods. These have different purposes and are used in different ways...."
I hope this helps.