Instance Method
Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in within which it defined.
public void geek(String name)
{
// code to be executed....
}
// Return type can be int, float String or user defined data type.
Memory allocation:Â These methods themselves are stored in Permanent Generation space of heap but the parameters (arguments passed to them) and their local variables and the value to be returned are allocated in stack. They can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depend on the access type provided to the desired instance method.
Important Points:
- Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
- Every individual Object created from the class has its own copy of the instance method(s) of that class.
- Instance methods are not stored on a per-instance basis, even with virtual methods. They’re stored in a single memory location, and they only “know” which object they belong to because the this pointer is passed when you call them.
- They can be overridden since they are resolved using dynamic binding at run time.
// Example to illustrate accessing the instance method . import java.io.*; class Foo{ String name = ""; // Instance method to be called within the same class or // from a another class defined in the same package // or in different package. public void geek(String name){ this.name = name; } } class GFG { public static void main (String[] args) { // create an instance of the class. Foo ob = new Foo(); // calling an instance method in the class 'Foo'. ob.geek("GeeksforGeeks"); System.out.println(ob.name); } } |
Output :
GeeksforGeeks
Static Method
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
public static void geek(String name)
{
// code to be executed....
}
// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.
Memory Allocation: They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Important Points:
- Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
- They are designed with aim to be shared among all Objects created from the same class.
- Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.
// Example to illustrate Accessing the Static method(s) of the class. import java.io.*; class Geek{ public static String geekName = ""; public static void geek(String name){ geekName = name; } } class GFG { public static void main (String[] args) { // Accessing the static method geek() and // field by class name itself. Geek.geek("vaibhav"); System.out.println(Geek.geekName); // Accessing the static method geek() by using Object's reference. Geek obj = new Geek(); obj.geek("mohit"); System.out.println(obj.geekName); } } |
Output:
vaibhav mohit
Note: Static variables and their values (primitives or references) defined in the class are stored in PermGen space of memory.
What if static variable refers to an Object ?
static int i = 1; static Object obj = new Object();
In first line, Â the value 1 would be stored in PermGen section. In second line, the reference obj would be stored in PermGen section and the Object it refers to would be stored in heap section.
When to use static methods ??
- When you have code that can be shared across all instances of the same class, put that portion of code into static method.
- They are basically used to access static field(s) of the class.
 Instance method vs Static method
- Instance method can access the instance methods and instance variables directly.
- Instance method can access static variables and static methods directly.
- Static methods can access the static variables and static methods directly.
- Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.
References
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Donât stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Understanding storage of static methods and static variables in Java
- Static and non static blank final variables in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- Understanding "static" in "public static void main" in Java
- Why can't static methods be abstract in Java?
- Can we Overload or Override static methods in java ?
- Instance Variable Hiding in Java
- Instance Initialization Block (IIB) in Java
- Instance variable as final in Java
- Shadowing of static functions in Java
- Are static local variables allowed in Java?
- Assigning values to static final variables in Java
- Comparison of static keyword in C++ and Java
- Static blocks in Java
- Static class in Java
- Static vs Dynamic Binding in Java
- static keyword in java
- Final static variable in Java
Improved By : temnur

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
