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.
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.
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.
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); } }
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.
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.