Monday, November 10, 2014

Use of static construct() method and static new() method of a class

Static Construct():
This method returns an instance of the class. It is a best practice to have a construct method in your class. This method should always be static but can be public or private depending upon the use. When calling a class from another class or job construct method can be used for this
Example:
To give you an example, let assume that we have a class ‘AsimConstructClass’ and have a variable ‘Name’ having a parmName method to be set or get
pic2
Here I have create a construct method from template, which is returning the instance
pic3
I have also create a run method for my logic in a class
pic4
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
pic5
Static New():
Basically new method is the default constructor of a class invoke when user uses ‘new’ keyword. Mostly it is protected like.
pic6
But in some cases we can create our own static new method like
The above example for construct method, shows an infolog with the name ‘Asim’ which is a hardcoded value in a class, So now what if  a user wants to send some input from a another class or job. In order to do this we can create your own individually named static new… methods with different parameter profiles and make it public static
Here newName takes a parameter,
pic7
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
pic8
And now in the job I did this
pic9
Finally on running it will show
pic10

No comments:

Post a Comment