Announcements
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.
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
*This post is locked for comments
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.
but why we should not use new method directly and instead use construct method
excellent
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)
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.
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156