Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why static functions are required?
Posted

 
Share this answer
 
Comments
Menon Santosh 20-Dec-12 8:05am    
+5
[no name] 20-Dec-12 8:18am    
Thank you santosh
static = not(dynamic)<br />
not(dynamic) = no instance specific<br />
no instance specific = class specific<br />
<br />
therefore, static = shared (run-time sharing of variables/objects between instances of a particular class.)


And for access those static variables/objects We require Static methods/Functions...

static variables/objects are class specific not instance-specific...
so that,
static variables & methods can directly call using classname.


real time example
you want to count how many objects created of particular class
then declare static variable and increment-decrements value when you are create new instance of that class & dispose that instance

Happy Coding!
:)
 
Share this answer
 
Comments
Menon Santosh 20-Dec-12 8:06am    
+5
Aarti Meswania 20-Dec-12 8:12am    
Thank you!
:)
Static function does not need an instance to call this method. In programming we normally use static method when we need same operation has to be called in different classes, different modules.

eg: Conrol validation has to be performed in diffrent UI class.

inorder write the same logic again and again you can define a static method

C#
class Validator
{
  static bool Validate(TextBox text)
  {
    bool success = false;

    if(!string.IsNullOrEmpty(text.Text))
    {
      success = false;
    }
   return success;
  }
}

so you can call this method as 
Validator.Validate(myTextBox);
Validator.Validate(mycomboBox); // this is an example

so you dont need to create an instance to call this method. Static methods are very handy.
You can find more generous answer from google or code project. I made it very simple for yours understanding.
 
Share this answer
 
Comments
Menon Santosh 20-Dec-12 8:06am    
+5
Jibesh 20-Dec-12 8:07am    
Thanks Santosh
Suppose you declare static variables in your class.
eg

C++
class Example
{
private:
int aVariable;
static int anotherVariable;
};

Example::anotherVariable = 0;

Now the property of the static Variable is that it doesn't belong to any particular instance of the class. It is shared by all the instances of the class. It exists even if there are no instances of the class.

Now suppose you would wish to access this variable. You could do that before creating any instances of the class. But how would you access it.

That is where static functions comes into play. With them you could access the static variable.

eg

C++
class Example
{
public:
static int returnStaticVariable()
{
return anotherVariable;
}
private:
int aVariable;
static int anotherVariable;
};


You could call this static function with the Class Name, instead of instance.
eg
C++
int main()
{
std::cout<<Example::returnStaticVariable();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900