Goldman Sachs Interview Question
Software Engineer / DevelopersA static method is not bound to an instance of a class. It does not need a class instance to access it. But to access other (non-static) members of the class from the static method, we need to declare a class instance inside static method.
plaese explain in detail with example,how can we acess the member of class without instance????
Question didn't ask about static specific to classes.
- Maninder Singh June 07, 2009We can explain a static method in terms of a class or as a normal non-class function.
A staic method in a class's role is to use the static data members only.
and staic method can be accessed by a static/non-static class object.
But a static method cannot have non-static access to the data-members.
And static method cnnot be constant, volatile and virtual.
Now, static non-class method: A staic keyword in front of a non-class method is used to limit the method scope to the current compilation unit. It has internal linkage and one can use this principal in C for information hiding, while class's static method/data-member has external-linkage.
e.g.
In abc.cpp/c
static void print_some_data()
{
}
is limited in abc.cpp/c only, no other compilation unit in an application can use it.