Implicit and Explicit Parameters

There are two types of methods in Java:

Implicit Parameters & Explicit Parameters

Explicit Parameters

public class Parameters {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JamesSquare james = new JamesSquare();
		james.getArea(20);
	}

}


class JamesSquare {
	public int getArea(int side){
		return side*side;
	}
}

“int side” in the getArea method is an EXPLICIT parameter.

Implicit Parameters

public class Parameters {

	public static void main(String[] args) {
		
		String str1="James";
		str1.length(); //5
		
		String str2="Bun";
		str2.length(); //3
	}
}

str1 is a reference which points to an object of type string.
str2 is a reference which points to an object of type string.
length() is a method that doesn’t take any arguments in the parentheses. There are no explicit parameters used here.
The length() method acts on a different object each time

The Object acts as an implicit parameter to the length() method.

Implicit parameters, another example


public class Parameters {

	public static void main(String[] args) {
		
		MyClass one= new MyClass();
		one.myName="James";
		
		// below, "one" becomes the implicit parameter so that
		// displayName() knows which name to display from WHICH OBJECT
		one.displayName();
		
		MyClass two= new MyClass();
		two.myName="Emma";
		
		// below, "one" becomes the implicit parameter so that
		// displayName() knows which name to display from WHICH OBJECT
		two.displayName();
		
		// so when two.displayName(); is called, displayName() below 
		// is concerned with the parameters that belong to "two"
	}
}


class MyClass {
	
	String myName;
	
	public void displayName(){
		// at RUNTIME, myName will consider "WHICH OBJECT CALLED ME"
		System.out.println(myName);
		
	}
}

In summary:

An implicit parameter is the object on which the method is called.

It is called implicit because we don’t declare it when we declare the method.

This Post Has 2 Comments

  1. Pingback: Skidson

  2. Pingback: My Site

Leave a Reply