Static methods and instance methods in Java (Usage Summary)

Example method

        An instance method is a method that needs to create an object of its class before calling. To invoke an instance method, we must create an object in which its class is defined.  

Memory allocation:  

        These methods themselves are stored in the permanent generation space of the heap, but the parameters (the parameters passed to them) and their local variables and the values to be returned are allocated in the stack. They can be invoked in the same class they are in, or can be invoked from different classes defined in the same package or other packages, depending on the type of access provided to the required instance methods.

main points:  

  • Instance methods belong to class objects instead of classes, that is, they can be called after creating objects of class.
  • Instance methods are not stored on a per instance basis, even if virtual methods are used. They are stored in a separate memory location, and they only "know" which object they belong to, because this pointer is passed when they are called.
  • They can be overridden because they are resolved at run time using dynamic binding.

The following is the implementation of the access instance method:

package org.jake.demo.utils;

public class InstanceUtils {

	public String name = "";

	/**
	 * An instance method that is called in another class defined in the same class or from the same package or different package.
	 * 
	 * @param name
	 */
	public void getName(String name) {
		this.name = name;
	}
}

package org.jake.demo;

import org.jake.demo.utils.InstanceUtils;

public class App {
	public static void main(String[] args) {
		InstanceUtils instanceUtils1 = new InstanceUtils();
		InstanceUtils instanceUtils2 = new InstanceUtils();
		instanceUtils1.getName("instanceUtilsName1");
		instanceUtils2.getName("instanceUtilsName2");
		System.out.println(instanceUtils1.name);
		System.out.println(instanceUtils2.name);

	}
}

Output:

instanceUtilsName1
instanceUtilsName2

Static method

        Static methods are methods that can be called in Java without creating class objects. They are referenced by the class name itself or by a reference to an object of that class.   

Memory allocation:  

        They are stored in the permanent generation space of the heap because they are associated with the class they belong to, not with the objects of that class. But their local variables and the parameters passed to them are stored on the stack. Because they are classes, you can call them without creating objects of the class.

main points:   

  • Static methods are associated with the class in which they are located, that is, there is no need to create an instance of the class when calling them, that is, ClassName.methodName(args).
  • They are designed to be shared among all objects created from the same class.
  • Static methods cannot be overridden because they are resolved by the compiler at compile time using static bindings. However, we can declare a method with the same name as static in superclasses and subclasses, but it will be called method hiding, because the derived class method will hide the base class method.

The following is the implementation of accessing static methods:  

package org.jake.demo.utils;

public class StaticUtils {
	public static String name = "";

	public static void getName(String name) {
		StaticUtils.name = name;
	}
}

package org.jake.demo;

import org.jake.demo.utils.StaticUtils;

public class App {
	public static void main(String[] args) {

		StaticUtils.getName("staticUtilsName");
		StaticUtils.getName("staticUtilsName");
		System.out.println(StaticUtils.name);
		System.out.println(StaticUtils.name);
	}
}

Output:

staticUtilsName
staticUtilsName

Note: the static variables defined in the class and their values (primitives or references) are stored in the permanent generation space of memory.  

In the first line, the value 1 is stored in the PermGen section. In the second line, the reference obj will be stored in the PermGen section, and the Object it references will be stored in the heap section.

When to use static methods?  

  • When you have code that can be shared between all instances of the same class, put that part of the code into a static method.
  • They are basically used to access static fields of a class.

Instance method vs static method

  • Instance methods can directly access instance methods and instance variables.
  • Instance methods can directly access static variables and static methods.
  • Static methods can directly access static variables and static methods.
  • Static methods cannot directly access instance methods and instance variables. They must use references to objects. And static methods cannot use this keyword because there is no instance referencing 'this'.

Error in this static field:

package org.jake.demo.utils;

public class StaticUtils {
	public static String name = "";

	public static void getName(String name) {
		this.name = name;
	}
}

package org.jake.demo.utils;

public class InstanceUtils {

	public String name = "";

	/**
	 * An instance method that is called in another class defined in the same class or from the same package or different package.
	 * 
	 * @param name
	 */
	public void getName(String name) {
		this.name = name;
	}
}

package org.jake.demo;

import org.jake.demo.utils.InstanceUtils;
import org.jake.demo.utils.StaticUtils;

public class App {
	public static void main(String[] args) {
		InstanceUtils instanceUtils1 = new InstanceUtils();
		InstanceUtils instanceUtils2 = new InstanceUtils();
		instanceUtils1.getName("instanceUtilsName1");
		instanceUtils2.getName("instanceUtilsName2");
		System.out.println(instanceUtils1.name);
		System.out.println(instanceUtils2.name);

		StaticUtils.getName("staticUtilsName");
		StaticUtils.getName("staticUtilsName");
		System.out.println(StaticUtils.name);
		System.out.println(StaticUtils.name);
	}
}

Output:

instanceUtilsName1
instanceUtilsName2
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Cannot use this in a static context

	at org.jake.demo.utils.StaticUtils.getName(StaticUtils.java:7)
	at org.jake.demo.App.main(App.java:15)

Note: thread safety and memory allocation should be considered when using instance methods and static methods as tool classes.

Tags: Java IntelliJ IDEA

Posted on Mon, 22 Nov 2021 00:40:22 -0500 by shakuni