Google Interview Question
Software Engineer / Developers-Singleton
-Utility Classes
-Factory Methods
Instance methods are instance methods because they rely on the state of the specific object instance.Any method that is independent of instance state is a candidate for being declared as static. The behavior instigated by a class method does not rely on the state of a particular instance. In fact, a static method cannot rely on an instance's state since static methods lack access to this reference. Instead, the behavior of a class method either depends on a state that all objects share at the class level, or is independent of any state at all.
javaworld.com/javaworld/javaqa/2001-11/03-qa-1121-mrhappy.html?page=1
Singleton - It is a design pattern used when we want to have only one instance of a class to be returned. e.g. someone asks for a connection to an instrument. Every time a person asks for a connection we would return the same instance we created.
In that case we can define an object as static (which would mean only one instance exist throughout the scope) and define the constructor as private so that no other method can create an instance of the class.
Apart from that static would be used for counter purpose.
Static member function can be used when creating new thread to run a member function.
class a {
Sorry, didn't finish in the original reply.
In PThread library, a normal function (i.e., not non-static-member-function) has to be provided as the parameter of pthread_create(). In this case, a static member function of the class of subject is useful.
class A {
public:
static void* thread_run(void *obj);
private:
void doSomeWork();
}
void *
A::thread_run(void* obj) {
A* ap = static_cast<A*>(obj);
ap->doSomeWork();
}
void
main() {
A a_obj;
pthread_t thread_1;
pthread_create(&tthread_1, NULL, (void* (*)(void*))&A::thread_run, static_cast<void*>(&a_obj));
}
(actually, the doSomeWork() function doesn't need to be a member of A, and the passed in object doesn't need to be class A, but the thread_run function has to be a static member function, if it needs to belong to some class)
1. singlton (of course, one can use non-member funcions too to create singlton)
- columbo October 26, 20102. to forbid to create objects on stack
3. automatic count amount of created objects and save it in private static data member and get the value of it by use of static member function
ideas have finished...