Skip to main content

Method Details

Here are some more details about class methods.

  • Although ObjectScript variables don't use datatypes, class methods do. The method signature declares any arguments along with their datatypes. If the class method returns a value, the signature declares the datatype of the returned value. The examples and exercises use basic datatypes such as %String, %Numeric, %Date, and %Boolean. Full coverage of these datatypes is beyond the scope of this tutorial.

  • By default, class methods are public. You can also create private class methods, using the Private keyword after the signature. You can only call private class methods from elsewhere in the same class.

  • The code is contained within curly braces. There can be spaces and line breaks in ObjectScript code nearly anywhere. Exceptions: you can't split a line between a command and its arguments, nor within a single-line comment.

  • Use // or ; to denote a comment. If a line of code also contains a comment, you must separate it from the rest of the line with at least one space, followed by the comment. For a multi-line comment, mark the beginning of the comment with /* and the end with */.

  • By default, the variables within a class method are private, which means they can't be seen outside the class method.

  • You can change any variables to be public by using the Publiclist keyword after the signature. Any class methods that declare a common set of public variables share these variables. However, use of public (shared) variables is no longer considered good programming practice; the recommended way to share variables is to pass them as arguments. This example, and the exercises in this tutorial, use some public variables for demonstration purposes only.


A line of code can have a label at the beginning (also called a tag). If a line has a label, you must separate it from the rest of the line with a tab or space. Labels are more commonly found in routines. By convention, the first line of a routine should have a label matching the name of the routine.

Examples of public and private methods:

VS Code - ObjectScript

/// examples for ObjectScript Tutorial
Class ObjectScript.Examples

/// demo of public and private methods, along with a public variable
ClassMethod PrivatePublic()
    do ..Private()   // call a private method
    do ..Public(9)   // call a public method

/// a private method with public variable a
ClassMethod Private() [Private, Publiclist = a]
    write !, "setting a"  set a = 1
    write !, "setting b"  set b = 2
    write !, "setting c"  set c = 3
    write !, "setting d"  set d = 4

/// a public method with an argument and a return value
ClassMethod Public(num as %Numeric) as %String
    write !, "my favorite number is ", num
    return "This is my return value!!!"
Copy code to clipboard
Testing using the Terminal

USER>do ##class(ObjectScript.Examples).PrivatePublic()

Enter a number: 4
setting a
setting b
setting c
setting d
my favorite number is 9

USER>set x = ##class(ObjectScript.Examples).Public(7)     

my favorite number is 7
USER>write x
This is my return value!!!
USER>do ##class(ObjectScript.Examples).Private()          

DO ##CLASS(ObjectScript.Examples).Private()
Copy code to clipboard
FeedbackOpens in a new window