Static Class Members in X++ Programming

This article explains how static class members work in X++—including static methods, fields, constructors, and how to use them effectively in your code.

What Are Static Members?

Static members in X++ are defined using the static keyword. These members belong to the class itself, not to any specific object (instance) of the class. Static members are shared across all instances and remain the same for the duration of a user’s session.

You should use static methods in cases where:

  • There’s no need to access instance-level (non-static) variables.

  • There’s no need to call non-static methods.

Static Methods

Static methods are also called class-level methods. They are created using the static keyword and can be called directly on the class—without creating an object.

Example: Validating a Software Key

Imagine you have a licensing system. Each software key is unique, but the logic to validate it remains the same across all keys. This shared validation logic is ideal for a static method.

				
					public class SoftwareKey
{
    public static boolean validateSoftwareKey(str _softwareKeyString)
    {
        // Add your validation logic here
        return false;
    }
}

				
			

To call the static method, use the class name followed by two colons :: and the method name:

				
					boolean isValid = SoftwareKey::validateSoftwareKey(yourSoftwareKeyString);

				
			

Static Fields

Static fields (or variables) are declared with the static keyword. These variables are shared across all instances of the class and represent data that belongs to the class rather than any object.

Static Constructors

A static constructor is a special method that is automatically executed once—before any static or instance members of the class are accessed. This ensures that static data is initialized properly.

Syntax:
				
					static void TypeNew()

				
			
  • You don’t manually call the static constructor.

  • It runs automatically, only once per user session.

  • It takes no parameters and must be declared as static.

Example: Singleton Pattern with Static Constructor

A singleton ensures that only one instance of a class exists during a session. Here’s how you can implement it in X++:

				
					public class Singleton
{
    private static Singleton instance;

    private void new()
    {
        // Private constructor prevents external instantiation
    }

    static void TypeNew()
    {
        instance = new Singleton(); // Only one instance is created
    }

    public static Singleton Instance()
    {
        return Singleton::instance;
    }
}

				
			

To use the singleton:

				
					Singleton singleObj = Singleton::Instance();

				
			

Calling Static Methods

You can call static methods like this:

				
					ClassName::MethodName();

				
			

These methods can be helpful for performing utility operations or interacting with shared resources like tables, without needing to instantiate a class.

Static vs. Instance Methods

  • Both static and non-static (instance) methods can exist in the same class.

  • Static methods cannot directly access instance variables or methods unless they create an object of the class.

  • You can call private constructors from within static methods using the new keyword.


Let me know if you’d like an infographic or web version of this content. I can also combine this with your previous blog on inheritance for a full “OOP in X++” guide.

Sharing Is Caring:

Leave a Comment

Top 30 JavaScript Interview Questions and Answers for 2024 Top 5 Front-End Web Development