- Home /
what does Static Function exactly do?
correct me if I'm wrong
well I've heard that if static it gives variable being global in the range that's allowed
if static only 1 exist and classes are made to have loads of them what's difference between static class/function
if it's wrong that I'm asking 2 Questions please tell me and I'll make 2 Thread Q
public = global
private = local
static = global (only 1 exist)
public static = global (only 1 exist)
private static = localized global (only 1 exist)
what's difference between
Static Class
Static Function
Answer by Bunny83 · Jan 28, 2013 at 01:54 AM
Basically all functions are actually "static" because the code of the function only exist once in the whole program. The difference is that member functions which "belong" to an instance of the class have an additional invisible parameter: "this".
If you call a member function you actually just call the function and the compiler implicitly passes the reference of the instance.
Static functions are functions without that implicit parameter, that's all.
If you use C# you might know "extension methods". They actually are implemented as member functions actually wotk.
For example:
//C#
public static class Vector3Extensions
{
public static Vector3 Scaled(this Vector3 aThis, Vector3 aScale)
{
return Vector3.Scale(aThis, aScale);
}
}
this extension would allow this:
Vector3 myVec = new Vector3(2,4,8);
Vector3 result = myVec.Scaled(new Vector3(2,3,1));
// result will contain (4, 12, 8)
When Scaled is executed the actual "context" object will be passed as additional parameter.
Inside "real" member functions the compiler provides additional syntactic suggar. Since your function belongs to a class instance the compiler interprets all variables it don't know as "this.varName"
Answer by Golan2781 · Jan 27, 2013 at 11:20 PM
A static class is simply a class with all its member variables/functions being static. If a static class has a function, that function is automatically static as well.
A static function does not require an instance of a class, similar to how a static variable does not either. Generally, it will only work with static variables of the same class or variables from other classes.
but static class does not require an instance either
so what's difference between static class and function
I see no difference
what more can static class do than static function?
A static class prevents you from accidentally adding a non-static member, and also prevents you from accidentally creating a new instance of it using the 'new' keyword.
Your answer
