EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Methods

Instance methods are methods that are called on an instance (object) of the class they belong to. To invoke an instance method, you first need to create an object of the class.

Example syntax:

public void display(String message) {
    // Code to be executed
}

Instance methods can return any data type, including primitive types (e.g., intfloat) or user-defined types.

Memory Allocation of Instance Methods

Instance methods themselves are stored in the Permanent Generation (PermGen) space of the heap until Java 7, but since Java 8, they are stored in Metaspace. However, their local variables and parameters (arguments passed) are stored in the stack.

These methods can be called from within the class in which they are defined or from other classes, depending on the access modifiers.

Key Points:

1. Instance methods belong to objects, not to the class itself.
2. They are stored in a single memory location and can access the object they belong to using the this reference.
3. Instance methods can be overridden, as they are resolved at runtime through dynamic binding.

Example:

// Example to demonstrate accessing an instance method.
class Car {

    String model = "";

    // Instance method to set car model
    public void setModel(String model) {
        this.model = model;
    }
}

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

        // Create an instance of the Car class
        Car myCar = new Car();

        // Call the instance method to set model
        myCar.setModel("Tesla Model S");
        System.out.println(myCar.model);
    }
}

Output:

Tesla Model S

Java Static Methods

Static methods, unlike instance methods, belong to the class rather than an object. They can be called without creating an instance of the class.

Example syntax:

public static void display(String message) {
    // Code to be executed
}

Static methods must include the static modifier in their declaration.

Memory Allocation of Static Methods

Static methods are stored in the Metaspace area (starting from Java 8) since they are associated with the class rather than any object. However, their local variables and arguments are stored in the stack.

Key Points:

1. Static methods are called using the class name, without the need for an instance.
2. They are shared across all instances of the class.
3. Static methods cannot be overridden, but they can be hidden by subclass methods if both superclass and subclass declare a static method with the same name. This is called Method Hiding.

Example:

// Example to demonstrate accessing static methods.
class MathUtil {

    public static double pi = 3.14159;

    // Static method to calculate the circumference of a circle
    public static double circumference(double radius) {
        return 2 * pi * radius;
    }
}

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

        // Access static method and field using class name
        double result = MathUtil.circumference(5);
        System.out.println("Circumference: " + result);

        // Access static method using an instance (though it's unnecessary)
        MathUtil util = new MathUtil();
        double result2 = util.circumference(7);
        System.out.println("Circumference: " + result2);
    }
}

Output:

Circumference: 31.4159
Circumference: 43.98226
End of lesson.