Implicit Explicit Parameters

Introduction

In Java methods, there are two types of parameters:

  • Explicit Parameters: Listed in method signature
  • Implicit Parameter: The object on which method is called (accessed via this)

Explicit Parameters

Explicit parameters are the parameters listed in the method declaration.

class Calculator {
    // a and b are explicit parameters
    int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        int result = calc.add(10, 20);  // 10 and 20 are arguments
        System.out.println(result);  // 30
    }
}

Implicit Parameter

Implicit parameter is the object on which the method is called. Referenced by this.

class Employee {
    String name;
    double salary;

    void display() {
        // 'this' is the implicit parameter
        System.out.println("Name: " + this.name);
        System.out.println("Salary: " + this.salary);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.name = "John";
        emp.salary = 50000;

        emp.display();  // emp is the implicit parameter
    }
}

Complete Example

class BankAccount {
    private double balance;

    // balance is implicit parameter (accessed via this)
    // amount is explicit parameter
    void deposit(double amount) {
        this.balance = this.balance + amount;
        // OR simply: balance = balance + amount;
    }

    void withdraw(double amount) {
        if (this.balance >= amount) {
            this.balance = this.balance - amount;
        } else {
            System.out.println("Insufficient balance");
        }
    }

    double getBalance() {
        return this.balance;  // implicit parameter
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();

        account.deposit(1000);    // account = implicit, 1000 = explicit
        account.withdraw(500);    // account = implicit, 500 = explicit

        System.out.println("Balance: " + account.getBalance());
    }
}

Understanding this

this keyword refers to the current object (implicit parameter).

class Student {
    String name;
    int rollNo;

    // Constructor with same parameter names
    Student(String name, int rollNo) {
        this.name = name;      // this.name = field, name = parameter
        this.rollNo = rollNo;  // this.rollNo = field, rollNo = parameter
    }

    void display() {
        // this refers to current object
        System.out.println(this.name + " - " + this.rollNo);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("John", 101);
        Student s2 = new Student("Alice", 102);

        s1.display();  // s1 is implicit parameter (this = s1)
        s2.display();  // s2 is implicit parameter (this = s2)
    }
}

Example: Comparison Method

class Employee {
    String name;
    double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    // this = implicit parameter (current object)
    // other = explicit parameter
    boolean earnMoreThan(Employee other) {
        return this.salary > other.salary;
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Employee("John", 50000);
        Employee emp2 = new Employee("Alice", 60000);

        if (emp1.earnMoreThan(emp2)) {
            System.out.println(emp1.name + " earns more");
        } else {
            System.out.println(emp2.name + " earns more");
        }
    }
}

Static Methods (No Implicit Parameter)

Static methods don’t have implicit parameter because they belong to class, not object.

class MathUtils {
    // Static method - no implicit parameter
    // Only explicit parameters: a and b
    static int add(int a, int b) {
        return a + b;
    }

    // Instance method - has implicit parameter (this)
    int multiply(int a, int b) {
        return a * b;
    }
}

public class Main {
    public static void main(String[] args) {
        // Static method - no object needed
        int sum = MathUtils.add(10, 20);

        // Instance method - object needed (implicit parameter)
        MathUtils utils = new MathUtils();
        int product = utils.multiply(10, 20);
    }
}

Method Call Analysis

class Rectangle {
    double length;
    double width;

    double getArea() {
        return this.length * this.width;
    }

    double compareArea(Rectangle other) {
        return this.getArea() - other.getArea();
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        r1.length = 5;
        r1.width = 3;

        Rectangle r2 = new Rectangle();
        r2.length = 4;
        r2.width = 6;

        // r1.compareArea(r2):
        // - r1 is implicit parameter (this)
        // - r2 is explicit parameter (other)
        double diff = r1.compareArea(r2);
        System.out.println("Difference: " + diff);
    }
}

When to Use this

Required:

class Person {
    String name;

    // MUST use this to distinguish
    Person(String name) {
        this.name = name;  // this.name = field, name = parameter
    }
}

Optional:

class Person {
    String name;

    void display() {
        System.out.println(this.name);  // Can use this
        System.out.println(name);       // Or omit this
    }
}

Summary Table

AspectImplicit ParameterExplicit Parameters
WhatCurrent objectListed in signature
AccessVia thisBy parameter name
CountAlways 1 (the object)0 or more
Static methodsNo implicit parameterOnly explicit
Exampleemp.display()emp is implicitadd(10, 20)10, 20 explicit

Complete Example

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;    // this = implicit, name = explicit
        this.price = price;  // this = implicit, price = explicit
    }

    // this = implicit parameter
    // discount = explicit parameter
    public void applyDiscount(double discount) {
        this.price = this.price - (this.price * discount / 100);
    }

    // this = implicit parameter
    // other = explicit parameter
    public boolean isCheaperThan(Product other) {
        return this.price < other.price;
    }

    public void display() {
        System.out.println("Name: " + this.name);
        System.out.println("Price: " + this.price);
    }
}

public class Main {
    public static void main(String[] args) {
        Product p1 = new Product("Laptop", 50000);
        Product p2 = new Product("Phone", 30000);

        p1.applyDiscount(10);  // p1 = implicit, 10 = explicit

        if (p2.isCheaperThan(p1)) {  // p2 = implicit, p1 = explicit
            System.out.println("Phone is cheaper");
        }

        p1.display();  // p1 = implicit
        p2.display();  // p2 = implicit
    }
}

Exam Tips

Remember:

  1. Implicit parameter: Object on which method is called
  2. Explicit parameters: Listed in method signature
  3. this keyword accesses implicit parameter
  4. Static methods: No implicit parameter
  5. Instance methods: Have implicit parameter
  6. Use this when parameter name = field name
  7. this optional when no naming conflict

Common Questions:

  • What is implicit parameter?
  • What is explicit parameter?
  • What is this keyword?
  • Difference between implicit and explicit?
  • Do static methods have implicit parameter?
  • When to use this?