I would like to know the difference between a static method and the same method defined as an instance method. I would like to know the difference in terms of performane and memory usage. Which one will be more efficient?
Method defined as static:
public static String getX()
By having a static method, it belongs to the class unlike instance method which belong to object that class.
In terms of performance and memory usage, static methods as said does not require you to create class object in order to access them but that does not mean its efficient too many static methods and static variables creates memory problem.
Overall use of static method and instance variables is based on type of requirement, say you want to have a utility like method in such case static method would be ideal while in other cases instance variable would fine as you will not have certain constraints as in static method
- static method can only call other static methods
- A static method must only access static data