Static Methods in Java | Instance Method vs Static Method

1. Objective

The last article was telling the story of Java Functional Interface. Here, we will study the static methods in Java. Moreover, we will see when we can use Java static methods. At last, we will discuss the Static vs Instance method in Java.

So, let us start Static Methods in Java.

Static Methods in Java

Static methods in Java | Instance Method vs. Static Method

2. What are Static Methods?

Static methods square measure the ways in Java which will refer to while not making an object of sophistication. They’re documented by the {class the category} name itself or relevancy the object of that class.

Do you know What is Clone() Method In Java?

3. Static Methods in Java

The syntax for static methods in Java –

public static void geek(String name)
{
// code to be executed....

a. Memory Allocation in Java Statics Methods

This stores in Permanent Generation space of heap as it associates to the {class the category} during which they reside to not the objects of that class. However, their local variables and also the passed argument(s) to them keep within the stack. Since they belong to the class so that they are often referred to as to while not making the object of the class.

b. Important Points

  • Static method(s) associated with the class during which they reside i.e. they will refer to as even while not making an instance of the class i.e.  ClassName.methodName(args).
  • They designe with the aim to share among all objects created from the same class.
  • Static methods can’t override. However are often overloaded since they’re resolved victimization static binding by the compiler at compile time.

Example for Static Methods In Java–

import java.io.*;
class Flair{
   public static String FlairName = "";
   public static void geek(String name)
{
       FlairName = name;
   }
}
class GFG {
   public static void main (String[] args) {
       Flair.flair("vaibhav");
       System.out.println(Flair.flairName);
       Flair obj = new Flair ();
       obj.flair("shadow");
       System.out.println(obj.flairName);
   }
}

Output:
vaibhav
shadow

Read What is Java Synchronized Method | Java Synchronized Block

Note: Static variables and their values (primitives or references) define within the class and stores in PermGen space of memory.
What if static variable refers to an Object?

static int i = 1;
static Object obj = new Object();

In the first line, the value one would store in PermGen section. In the second line, the reference obj would store in PermGen section and also the object it refers to would store in the heap section.

Java Quiz

4. When to Use Java Static Methods?

  • When you have code that may shares across all instances of the same class, place that portion of code into a Static method.
  • Primarily accustom access static field(s) of the class.

5. What is Java Instance Method?

Instance method aisthe ways that need an object of its class to create before it will refer to as. To invoke an instance method, we’ve to make an Object of the category in within that it defined.

public void flair(String name)
// code to be {executed....
}
// return type can be int, float String or user defined data type.
  • Memory allocation: These methods themselves are kept in Permanent Generation space of heap. However, the parameters (arguments passed to them) and also their local variables and the value to return are allotted in a stack.

Learn StringTokenizer In Java | Constructors, and Methods

Important Points:

  • Instance method(s) belong to the object of the class to not the class i.e. they will refer to as after making the object of the class.
  • Every individual Object created from {the class, the category} has its own copy of the instance method(s) of that class.
  • They can overridden since they’re resolved using dynamic binding at runtime.

6. Java Instance Method vs Static Method

  • Instance method will access the instance methods and instance variables directly.
  • Instance method will access static variables and static methods directly.
  • Static methods will access the static variables and static methods directly.
  • Static methods can’t access instance methods and instance variables directly. They have to use respect to the object. And static method can’t use this keyword as there’s no instance for ‘this’ to refer to.

So, this was all about the Static method in Java. Hope you like our explanation.

7. Conclusion

Hence, in this Java Static method tutorial, we saw what are static methods in Java. Moreover, we discussed memory allocations in Java static methods. Also, we discussed when to use static methods in Java.

See also – Method Overloading in Java with Examples

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.