Static and Instance Methods in Java

Static and Instance Methods in Java
Static and Instance Methods in Java

Introduction

Java is one of the most popular object-oriented programming languages. Every Java program must be enclosed inside a java class. Classes in Java are usually made up of data members and methods. The topic of methods is a vast one because Java gives them so much power and flexibility. 

This article will discuss static methods vs instance methods in Java. There are some basic rules that you need to learn to begin to add methods to your classes. 

This blog will explain everything about static methods vs instance methods in Java. 

So, let’s get started.

General Form in Java

The general form of methods in Java is given below:

<Modifier> <Return type> <name_of_method>(parameter list) 
{
// body of the method
}

Here,

  • The modifier can be any access modifier such as public, private, protected or default. Or it can be a non-access modifier such as final, static, abstract etc.
  • Return type specifies the type of data returned by the method. The return type can be any valid type, including the class type. 
  • The name of the method is specified by name_of_method. It can be any legal identifier other than those already used by other items within the current scope.
  • The parameter list is a sequence of type and identifier pairs separated by commas. Parameters are variables that receive the value of the arguments passed to the method when called. 

Static Methods in Java

Usually, the methods of a class are dependent on the object of the class. But, there will be times when we want to define a method inside a class that can work independently. That is they are independent of the objects of the class.

In Java, it is possible to create a method inside a class that can be used by itself, without reference to an instance of the class. This type of method is known as the static method.  To create such a method, we’ve to precede its declaration with the keyword static

When a member is declared static, our program can access it before any class objects are created, i.e. without reference to any object. The most common example of a static member is the main() method. The main() method is declared static because it must be called before any objects exist.

Restrictions

  • They can only call other static methods inside them.
  • They must only access static data.
  • They cannot refer to this or super in any way. 

Note: The keyword super is used in inheritance. And, this is used to refer to the instance of the current class.

Example

 public class StaticDemo 
 {
  static int a = 13;
  static int b = 1199;
  static void callme() 
  {
  System.out.println("The value of a = " + a);
  }
}
 public class TestThis
 {
  public static void main(String args[]) 
  {
  //Static method is being called using the name of the class
  StaticDemo.callme();
  //Static variable is accessed with the class name
  System.out.println("The value of b = " + StaticDemo.b);
  }
}

Output:

The value of a = 13
The value of b = 1199

Memory Allocation of Static Methods

The static methods get memory in the static section of the heap area. 

Instance Methods in Java

Although it is perfectly fine to define a class with only data members, it rarely happens. Most of the time, we use methods to access the instance variables of the class. 

Methods allow the class to hide the specific layout of internal data structures behind method abstractions. In addition to defining methods that provide access to data, we can also define methods used internally by the class itself.

An instance method is one such type of method. The instance method belongs to instances of a class, not to the class itself. Let’s understand the instance method with the help of an example.

Example

In the below program, the callme() instance method is being called with the object of the class.

 public class InstanceDemo 
 {
  static int a = 13;
  int b = 1199;//instance variable
  void callme() //instance method
  {
  System.out.println("The value of b = " + b);
  }
}
 public class TestThis
 {
  public static void main(String args[]) 
  {
  
  InstanceDemo id = new InstanceDemo();
  //Instance method is being called using the object of the class
  id.callme();
  //Static variable is accessed with the class name
  System.out.println("The value of b = " + InstanceDemo.a);
  }
}

Output

The value of b = 1199
The value of a = 13

Let’s see some of the main differences between static and instance methods in Java.

Differences: Static methods vs Instance methods in Java

The table below shows the fundamental differences between static methods vs instance methods in Java.

Static MethodsInstance Methods
Static methods can be called without the object of the class.Instance methods require an object of the class.
Static methods are associated with the class.Instance methods are associated with the objects.
Static methods can only access static attributes of the classInstance methods can access all the attributes of the class.
A static method is declared with the static keyword.Instance methods do not require any keyword.
The static method will exist as a single copy for a class.The instance method exists as multiple copies depending on the number of instances of the class.

Frequently Asked Questions

Can instance variables be static in Java?

Yes, instance variables can be declared as static in Java.

What is a static instance in Java?

Static instances are associated with the class rather than the object of the class. They can be accessed by using the class name.

What is the difference between static methods vs instance methods in Java?

Static members are independent of the objects of the class, while instance members depend on the object.

What is the instance method?

The instance method is a method that is declared in the class without using the static keyword. It can be called with the help of the object of the class.

Can we access instance members in the static method?

Key Takeaways

This article has cleared most of the doubts about static methods vs instance methods in Java. If you want to learn more about the static keyword, here’s a fantastic blog: What is a Static Keyword In Java?

Are you pondering whether or not you should learn Java in 2021? 

The answer is simple: Yes!

As the world becomes more reliant on mobile apps, Java becomes increasingly important as a programming language. It’s one of the most potent languages we’ve seen, with recruiters ranking it third in the last two years.

Here’s a fantastic course to learn Java along with Data structure and Algorithms offered by Coding Ninjas. Do check it out. 

Happy Learning!

By: Vaishnavi Pandey