Monday, July 2, 2018

Difference between Construct and New method in AX 2012

Before proceeding further let's understand what is Static Methods:

Static Methods:

Normally you can't call a method of a class without first creating an instance of that class.

By declaring a method using the static keyword, you can call it without first creating an object because it becomes a class method (i.e. a method that belongs to a class rather than an object).

Static methods are used for methods that do not need to access to an object's state or only use static fields. For example, the main method is a static method.

  • In X++ you can decorate a method with the static keyword. 
  • This keyword instructs the system to create only one instance of the method regardless of how many instances of its class you create. 
  • This single instance is used throughout your Microsoft Dynamics AX session.

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.

Static Construct Methods:

  • You should create a static construct method for each class. 
  • The method should return an instance of the class.

The construct method must be:


named "construct"

In most cases the construct method should be public. 
If the class is instantiated using a newParameter method, then declare the construct method as private

The method should return an instance of the class and contain no other code. construct methods should not have any parameters, or else should have the same parameters as the default new constructor (it is recommended that you do not have parameters on new).

If your class declaration contains parameters, use a static new method as the constructor and use the construct method within the static new method to create an instance of the class.

For partners and customizers, this is the point to add construction functionality for new subclasses (in higher layers), without mixing code with the construct method in the original layer.

Example :

let assume that we have a class ‘AsimConstructClass’ and have a variable ‘Name’ having a parmName method to be set or get

Here I have create a construct method from template, which is returning the instance

I have also create a run method for my logic in a class

Now, finally I have created a job and in this I will get the instance of the class and call the run method in a single call


static new... Methods:

  • The new method is the default constructor for a class. 
  • The new method should be protected.
  • Do not use it to instantiate the class. Use a construct or a static new… method instead.

It is recommended that you do not have parameters on the new method—use static new… methods instead.

X++ does not support method-name overloading. You must create your own individually named static new… methods with different parameter profiles. 

This enables you to construct a class in more than one way. Similarly, instead of creating default parameters in a new method, create a different static new… method for each possible parameter profile.

If you have created the new method on a subclass, call super() to carry out any necessary initialization that might be implemented in the superclass. 

The call to super() should be the first statement in the method.

static new… Methods
Create one or more static new… methods to instantiate your class.

These new methods have the following characteristics:
  • Are public
  • Are static
  • Have a name prefixed with "new"
  • Are named according to the parameter(s) they take, or logically
  • Usually take only nondefault parameters
  • Always return a valid object of the class's type (instantiated as well as initialized), or throw an error
  • Use a construct method to create an instance of the class
  • Use accessor methods to set the class variables
Here newName takes a parameter,


Also in the newName code, it is initializing the instance by calling the construct() method which should be private in this case ,so I made it private


Finally on running it will show




    Collection of Microsoft ERP Clips || ERP Licensing, Technical, Functional ,Manager Learning Videos Listing.

  2. I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. Calgary Roofing Contractor


How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...