Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Static method new and new method and static construct method

Posted on by Microsoft Employee

Hello everyone

Can anyone explains what are differences between 

Static new method and new method and static construct method

i saw article in MS says that

https://msdn.microsoft.com/en-us/library/aa854210.aspx

  • Each class must have a single public construction method unless the class is abstract. If no initialization is required, use a static construct method. Otherwise, use a static new… method (the default constructor (new method) for the class should be protected).

  • Each class should have at least one static construct method method. Warning icon

  • Each class should have at least one static new… method.

  • Each class should have a new method (the default constructor). This method should be protected.

- point no 3 and 4 speaks about static new and new !!! can any one explain difference between static new and new
- difference between construct method and static new and why static new should be protected 

- what advantage of using construct instead of static new 

Thanks for all members

  • Martin Dráb Profile Picture
    Martin Dráb 228,625 Most Valuable Professional on at
    RE: Static method new and new method and static construct method

    There often is a good reason to make the constructor protected and offer a public construct() method instead, therefore everybody is forced to call the static method instead of the constructor. For example, imagine that you've implemented a child class that you want to use instead of the parent class. You have to change all places where the constructor is used, which is trivial if the only place is in construct() instead of spread across the whole codebase.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Static method new and new method and static construct method

    but why we should not use new method directly and instead use construct method  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Static method new and new method and static construct method

    excellent

  • Verified answer
    Vilmos Kintera Profile Picture
    Vilmos Kintera 46,149 on at
    RE: Static method new and new method and static construct method

    Maybe they meant main method for the static call, since that is the entry point for any class when it is being opened via a menu item, where you instantiate it with a constructor (that creates a new instances using the new() method), initialization, then executing the main business logic within the class. Or it could be that if the class is extending another one, then on the parent you'd have the protected new method, then on the child which you'd normally instantiate you can have the regular new method with parameters, however that would still not be static.

    msdn.microsoft.com/.../aa673265.aspx

    So your class should normally have these if it could be executed via a menu item, and depending on parametrization or what other frameworks does it extend (such as RunBase or SysOperation):

    - main

    - new

    - contruct

    - init

    - run

    - (validate)

    - (parm*)

    - (pack/unpack)

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,625 Most Valuable Professional on at
    RE: Static method new and new method and static construct method

    The new "method" is a constructor, which is a special language construct for creating objects. You use it like this: new Object().

    Everything else you mentioned are just normal static methods, without any special meaning in the language. They're "factory" methods, i.e. they're also used to create objects, but they depend on constructors to do that. Names like construct() and new...() (e.g. newFromParameters()) are mere conventions.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,556 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,625 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans