static method vs instance method c# code example

Example 1: static method vs instance method

static or class method
1) A method that is declared as static is known as the static method
2) We don't need to create the objects to call the static methods
3) Non-static (instance) members cannot be accessed in static context 
(static method, static block and static nested class) directly.
4) For example: public static int cube(int n){
return n*n*n*; (multiply *) }

Instance method
1) A method that is not declared as static is known as the instance method.
2) The object is required to call the instance method.
3) Static and non-static variables both can be accessed in instance methods.
4) For example: public void msg() {
...}.

Example 2: static methods vs instance methods

STATIC METHODS: belongs to the class. 
Static methods I can call it through the class.
Very easy to use it.
You do not need to object to call it. 
In my framework, getmethod in my driver class is static. 
From the Configuration Reader Class, 
you can call Getpropertymethod throught 
the class name directly is static. 
All methods we are creating custom methods are static. 
We can use only static in custom methods nothing else. 
Static method we cannot use instance variables.
NON-STATIC METHODS: means it belongs to the object. 
you can not call it through class name 
If you have instance method, you can use any variables inside 
that instance methods. 
Only the Instance method can be overridden in Java.

Tags:

Java Example