Static Method: These methods are not belongs to class rather than object or instance and always call by the reference of class name. Class/Static method never calls instance methods and variables.
class TryCatch {
// Class/Static method implementation
public static int doSum(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
// Calling class/static method in same class.
doSum(2, 5);
}
}
public class TryCatchMain {
public static void main(String[] args) {
// Calling class/static method in other class.
TryCatch.doSum(2, 5);
}
}
Instance Method: These methods are part of instance or object and always calls by using refrence of same class. Instance methods can call a class/static method or valriable.
class TryCatch {
int i = 0;
//Class/Static method implementation
public static int doSum(int a, int b) {
int sum = a + b;
return sum;
}
public static void main(String[] args) {
//Calling class/static method in same class.
doSum(2, 5);
}
//Instance method implementation
public int doSum(int a, int b, int c) {
//Calling class/static method in instance method.
doSum(2, 5);
int sum = a + b;
return sum;
}
}