Booking.com Interview Question
Software Engineer / DevelopersCountry: Netherlands
In object oriented languages, you can create instances of classes (instance objects) in you have access to instance methods. The instance methods are defined within the class that instance object represents.
Depending on the programming language, you can have static class methods that can be called outside the context of an instance, however those methods cannot use the "this" keyword since there is no instance that you can reference within the method.
Class methods (C++ terminology: static method) are available for use without a concrete object of the class.
Instance methods are available to use only when there's a concrete object that will operate this method. Also, since there is a concrete object involves, the instance function can access and modify the object's members. Class methods cannot do that, since there is no object to manipulate.
A good example of a class method is object creation. The knowledge of how to create an object in encapsulated within the class.
STATIC METHODS VS Instance Method
- karthiga.m1988 February 25, 2013SM : When a method declaration includes a static modifier, that method is said to be a static method.
IM : When no static modifier is present, the method is said to be an instance method.
SM : Static method is the only one. All the instances will be accessing the same method
IM : Every instance of the class will have its own instance methods
static members as belonging to classes
Instance members as belonging to objects (instances of classes).
SM : When a static member M is referenced in a member-access of the form E.M, E must denote a type containing M.It is a compile-time error for E to denote an instance.
IM : When an instance member M is referenced in a member-accessof the form E.M, E must denote an instance of a type containing M. It is a compile-time error for E to denote a type.
SM : Static methods just operate on input parameters and cannot access internal instance fields.
IM : Instance method can acces the instance fields
SM : The static method is callable on a class even when no instance of the class has been created.
IM : Cannot access instance method with out the instance
SM : Static methods cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
IM : Instance methods can access static fields
SM : Static methods can be overloaded but not overridden
IM : Instance methods can be overriden when the class containing the instance method is inherited and having virtual keyword in the base class method and overriden key word in the derived class method
SM : A call to a static method generates a call instruction in Microsoft intermediate language (MSIL)
IM : call to an instance method generates a callvirt instruction, which also checks for a null object references
Example :
class Test
{ static void Main() {
int x; Test t = new Test();
static int y; t.x = 1; // Ok
void F() { t.y = 1; // Error, cannot access static member through instance
x = 1; // Ok, same as this.x = 1 Test.x = 1; // Error, cannot access instance member through type
y = 1; // Ok, same as Test.y = 1 Test.y = 1; // Ok
} }
static void G() { }
x = 1; // Error, cannot access this.x
y = 1; // Ok, same as Test.y = 1
}
Source : msdn