Understanding Static Methods in X++: Usage and Best Practices

Rumman Ansari   Software Engineer   2024-07-18 09:22:30   7073  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Static methods, which are also known as class methods, belong to a class and are created by using the keyword static. You don't have to instantiate an object before you use static methods. Static methods are often used to work with data that is stored in tables. Member variables can't be used in a static method.

Static methods are generally intended for cases where the following criteria are met:

  • The method has no reason to access the member variables that are declared in the classDeclaration block of the class.

  • The method has no reason to call any instance (non-static) methods of the class.

If we will create a static method then we don't need to create any object of the class in which the static method exists.

Code: Class Declaration


// Fruit Class declaration
public class Fruit
{
  // no need to declare anything
}


Code: Staic method inside a class Fruit



public static void methodStatic()
{
    str name1 = 'hello';
    info("This is static method "); 
    info(strFmt("%1", name1));
    
}


Code: Access a static method from main method


public static void main(Args _args)
{
        Fruit::methodStatic();
 }

We can't access a class member or class attribute from a static method.

Static Method Characteristics

  • Static methods cannot access instance-specific data or instance-specific methods, as they do not have a reference to an instance of the class.
  • Static methods can't be overridden, because they exist per class.
  • Static methods can be used for various purposes, such as performing calculations, creating utility functions, or initializing class-specific data.

Static methods can't be overridden, because they exist per class.

VehicleClass

class VehicleClass
{
   // Instance fields.
   real height;
   real width;
  
   // Constructor to initialize fields height and width.
   void new(real _height, real _width)
   {
       height = _height;
       width = _width;
   }
 
   static void message(){
           Info("I am inside message");
   }
 
   void message1()
   {
       Info("I am inside message");
   }
 
   static real add(real number1, real number2)
   {
      return number1+ number2;
   }
}

Car


class Car extends VehicleClass
{
   static void message()
   {
       //A call to 'super()' is not allowed here. 
       super(); // You will get error
       Info("I am inside message");
   }
 
   void message1()
   {
       super();
       Info("I am inside message");
   }
 
}


AccessVehicleClass

internal final class AccessVehicleClass
{
   /// <summary>
   /// Class entry point. The system will call this method when a designated menu 
   /// is selected or when execution starts and this class is set as the startup class.
   /// </summary>
   /// <param name = "_args">The specified arguments.</param>
   public static void main(Args _args)
   {
      // VehicleClass obj = new VehicleClass(12.0, 10.0);
       VehicleClass::message();
       Info(strFmt("%1", VehicleClass::add(12.0, 10.0)));
 
       Car::message();
   }
 
}