Wednesday, June 10, 2009
static Vs instance methods
12:38 PM |
Posted by
Sheen |
Edit Post
Perfomance , instance methods are not virtual by default but can be declared as virtual. Virtual method has slightly high overhead than a non virtual method. Static methods are not virtual.
So there might be a slight performance issue and this can be ignored as it's not make much impact on performance.
Choosing your method to be static or instance will depend upon the design. If the method going to be used as an object then it should be an instance method. If it doesn't depend on an instance of an object, it should be static. Static methods belong to a type and instance methods belong to instance of a type.
example
Public Class Car
{
// this is a static method
public static void staticMethod()
{
//your code
}
// this is an instance method
public void instanceMethod()
{
//your code
}
}
Accessing static method
Car.staticMethod();
Accessing instance method
Car car = new Car();
car.instanceMethod();
So there might be a slight performance issue and this can be ignored as it's not make much impact on performance.
Choosing your method to be static or instance will depend upon the design. If the method going to be used as an object then it should be an instance method. If it doesn't depend on an instance of an object, it should be static. Static methods belong to a type and instance methods belong to instance of a type.
example
Public Class Car
{
// this is a static method
public static void staticMethod()
{
//your code
}
// this is an instance method
public void instanceMethod()
{
//your code
}
}
Accessing static method
Car.staticMethod();
Accessing instance method
Car car = new Car();
car.instanceMethod();
Subscribe to:
Post Comments (Atom)
Blog Archive
Important Blogs
-
-
Git Workflow For Enterprise development6 years ago
-
-
Watch Live Cricket Online Free14 years ago
-
-
-
1 comments:
Good work mate!! Keep it up!
Post a Comment