General X++ Development

How do I create a singleton class in X++?

Asked by Sunny Vasty· Aug 13, 2026· 1 answers

How do I create a singleton class in X++?

SSunny Vasty75 reputation · answered Aug 13, 2026

Use a private constructor and a static accessor that returns the shared instance.

X++
final class MySingleton
{
    private static MySingleton instance;

    private void new()
    {
    }

    public static MySingleton instance()
    {
        if (!instance)
        {
            instance = new MySingleton();
        }

        return instance;
    }
}

Only use a singleton when there should genuinely be one shared instance. Do not use it just because passing an object around feels inconvenient.