Difference between Static Method and Non-Static Method




Static Method

  • Static method is a method that belongs to the class, not to the instance of the class.
  • It can be access without creating the object of the class.
  • Static method can only access static variables, static methods of same class or another class.
  • Method Overriding – Static methods cannot be overridden because of early binding 
  • Memory Allocation – In static method, memory allocation happens only once, because the static keyword fixed a particular memory for that method in ram. So when the method calls every time in a program, each time that particular memory is used.

Non Static Method

  • Non Static can be public, private, protected or default.  They do not have static or non-static keyword before their method name. 
  • It can be access by creating the object of the class.
  • Non Static method can access static variables, static methods of same class or another class as well as non-static variables and methods.
  • Method Overriding  – Non Static methods can be overridden because of runtime binding 
  • Memory Allocation – In non-static method, memory allocation happens when the method invokes and the memory is allocated every time when the method is called. So more memory is used here as compared to static method

Below is an example that shows how the static methods can be use without creating an object of the class. 

public class StaticMyClass {
    static void MyStatic_Method() { // Static Method
          System.out.println("Static method can be accessed without creating object");
     public void MyPublic_Method() () { // Public Method
          System.out.println("Public  method can be accessed only by creating object");
     public static void main(String[] args) {
          MyStatic_Method(); // Calling Static Method
          MyPublic_Method(); // Error - Complilation - can't make static reference to non-static method
          StaticMyClass obj1 = new StaticMyClass();


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s