Static and non-static methods are two different types of methods in Java that have different properties and behaviors. In this post, we will explain what these two types of methods are, how they differ from each other, and when to use them in our code.

1. Overview of static methods

A static method is a method that belongs to the class itself, and not to any specific object or instance of that class. We can think of a static method as a common behavior that is shared by all objects of that class, regardless of their individual characteristics. For example, Math.sqrt() is a static method that calculates the square root of a number, and it can be called without creating a Math object.

To declare a static method in Java, we need to use the keyword static before the return type of the method. To call a static method in Java, we need to use the class name followed by a dot (.) and then the method name. We do not need to create an object of the class to call a static method. For example:

Download  Run Code

 
A static method has the following characteristics:

  • It can only access static variables and other static methods within the same class. It cannot access non-static variables or methods directly.
  • It can be overridden by another static method in a subclass, but this is not considered as polymorphism.
  • It cannot use the keyword this or super, as they refer to the current object or the parent object, which do not exist for a static method.

2. Overview of non-Static methods

A non-static method is a method that belongs to each object or instance of the class, and not to the class itself. We can think of a non-static method as an instance-specific behavior that depends on the state or attributes of the object that calls it.

A non-static method can be called only by creating an object of the class, by using the object reference followed by the method name. For example, String.toUpperCase() is a non-static method that converts a string to uppercase, and it can be called only by creating a String object.

To declare a non-static method in Java, we do not need to use any keyword before the return type of the method. By default, every method in Java is non-static unless specified otherwise. To call a non-static method in Java, we need to create an object of the class using the new keyword, and then use the object reference followed by a dot (.) and then the method name. For example:

Download  Run Code

 
A non-static method has the following characteristics:

  • It can access both static and non-static variables and methods within the same class. It can also access non-static variables and methods of the parent class through inheritance.
  • It can be overridden by another non-static method in a subclass, and this is considered as polymorphism.
  • It can use the keyword this to refer to the current object, and super to refer to the parent object.

3. Differences between Static and Non-Static methods

Now that we have seen what static and non-static methods are in Java, let us summarize some of their key differences:

  1. A static method belongs to the class itself, while a non-static method belongs to each class instance. Therefore, a static method can be called directly without creating an instance of the class, and an object is needed to call a non-static method.
  2. Static methods cannot access non-static member variables (aka instance member variables) without creating an instance of its class. Static methods can only access static variables while non-static methods can access both static and non-static variables.
  3. Like non-static member variables, we cannot access any non-static method from a static method without creating an instance of its class. On the other hand, we can access a static method from both static and non-static methods in Java.
  4. A static method use early binding (or compile-time binding), whereas a non-static method requires runtime binding. This is the reason why we can call a static method without creating any instance.
  5. We cannot override a static method in Java while overriding a non-static method. This is because overriding require runtime binding (Polymorphism), and static methods are bonded by the compiler.
  6. We cannot use this and super keywords inside a static method since they are associated with a particular instance.
  7. Static methods reduce the memory footprint of the application. This is because the memory is allocated only once for the static method during class loading. In contrast, for a non-static method, memory is allocated every time the method is called.
  8. Static methods are declared using the keyword static, while non-static methods are declared without any keyword.
  9. Static methods are called using the class name followed by a dot (.) and then the method name, while non-static methods are called using an object reference followed by a dot (.) and then the method name.
  10. Static methods have a global scope and can be accessed from anywhere within the program, while non-static methods have a local scope and can be accessed only through an object of the class.
  11. Static methods are commonly used for utility methods, constants, or variables that are not specific to individual instances, while non-static methods are used for instance-specific behavior, as they hold data specific to each object.

4. When to use them in our code?

Both static and non-static methods have their own advantages and disadvantages, and we should use them according to our needs and preferences. Here are some general guidelines on when to use them in our code:

  • Use static methods when we need to perform operations that do not depend on the state or behavior of individual objects, but rather on some common logic or constant values. For example, utility methods, mathematical functions, or constants.
  • Use non-static methods when we need to perform operations that depend on the state or behavior of individual objects, as they hold data specific to each object. For example, instance variables, getters and setters, or business logic.
  • Use static methods when we need to access or modify static variables or other static methods within the same class, as they are shared among all objects of the class.
  • Use non-static methods when we need to access or modify non-static variables or other non-static methods within the same class or through inheritance, as they are unique to each object of the class.

That’s all about the static and non-static methods in Java.

 
Also See:

Static and non-static variables in Java