Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a method in Java?
3.
Instance methods in Java
3.1.
Example
3.2.
Java
3.3.
Memory Allocation of Instance Method
4.
Static Methods in Java
4.1.
Restrictions
4.2.
Example
4.3.
Java
4.4.
Memory allocation of static methods
5.
Difference Between Static methods Instance methods in Java
6.
Frequently Asked Questions
6.1.
What is the instance method? 
6.2.
What is a static instance in Java?
6.3.
What is difference between static method and instance method?
6.4.
Is an instance method a function?
6.5.
What is an instance method example?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Static and Instance Methods in Java

Introduction

Instance methods in Java are functions defined within a class that require an object of that class to be created before calling. In contrast, static methods are functions that can be called directly from the class itself, without needing an object instance.

Static and Instance Methods in Java

What is a method in Java?

In Java, a method is a block of code within a class that performs a specific task. Methods are the building blocks of Java programs and allow you to encapsulate functionality for reuse. 

They consist of a method signature, including the method's name, return type, parameters, and a method body enclosed in curly braces. Methods can be called (invoked) from other program parts to execute their code, making Java programs modular and organized.

Also read about - Types Of Methods

Instance methods in Java

Instance methods in Java are functions defined within a class that operate on instances (objects) of that class. These methods can access instance variables and are invoked using an object of the class. They provide behavior specific to individual objects and can modify the state of the object they belong to.

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.

  • Java

Java

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 a = " + InstanceDemo.a);
}
}

 

Output

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

 

Memory Allocation of Instance Method

In Java, instance method memory allocation involves creating a method frame on the call stack when the method is invoked. This frame holds local variables and parameters. Instance variables, part of the object's memory on the heap, are accessed using the `this` reference. After execution, the frame is removed, and unused memory is eventually garbage collected.

Static Methods in Java

Static methods in Java are methods that belong to a class rather than to instances of the class. They can be called directly on the class itself without needing to create an object. Static methods cannot access instance variables directly, but they can access static variables and other static methods. They are commonly used for utility functions or operations that don't require access to instance-specific data.

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

  • Java

Java

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

 

You can also read about the topic of Difference between argument and parameter 

Memory allocation of static methods

In Java, memory allocation for static methods is different from instance methods. Static methods are associated with the class, not instances, so they don't have a `this` reference. Memory for static methods is allocated on the call stack when the method is invoked, and it's deallocated after execution.

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

You can also compile the java code here.

Must Read Type Conversion in Java

Difference Between Static methods Instance methods in Java

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

Static Methods

Instance 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 class Instance 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.

Must Read Static Blocks In Java

Frequently Asked Questions

What is the instance method? 

An instance method in a class, declared without "static," is accessed using class objects, allowing it to operate on instance-specific data.

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 difference between static method and instance method?

A static method belongs to the class itself and can be called directly on the class, while an instance method operates on instances (objects) of the class and can access instance variables.

Is an instance method a function?

Yes, an instance method in Java is a type of function defined within a class that operates on instances (objects) of that class.

What is an instance method example?

An example of an instance method in Java is the toString() method in the String class. It is used to return the string representation of the object on which it is called. For example: String str = "Hello"; str.toString();

Conclusion

Java's static and instance methods serve different purposes in programming. Static methods are class-level and don't require object instantiation, while instance methods are tied to objects and their attributes. Choosing the right method type depends on the specific needs of your program, providing flexibility and organization.

Recommended Reading: 

Abstract class in java

Solid Principles in java

Also check out the Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.

Refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package

Happy Learning!

Live masterclass