Tuesday, October 22, 2019
Implicit Parameters in Java
Implicit Parameters in Java The implicit parameter in Java is the object that the method belongs to. Its passed by specifying the reference or variable of the object before the name of the method.à An implicit parameter is opposite to anà explicità parameter, which is passed when specifying the parameter in the parenthesis of a method call. If a parameter isnt explicitly defined, the parameter is considered implicit. Explicit Method Example When your program calls a method of an object, its common to pass a value to the method. For example, here, the object Employee has a method called setJobTitle: Employee dave new Employee(); dave.setJobTitle(Candlestick Maker); The String Candlestick Maker is an explicit parameter being passed to the setJobTitle method. Implicit Method Example However, there is another parameter in the method call that is known as the implicit parameter. The implicit parameter is the object the method belongs to. In the above example, its dave, the object of type Employee. Implicit parameters are not defined within a method declaration because they are implied by the class the method is in: public class Employee {à à public void setJobTitle(String jobTitle)à à {à à à à this.jobTitle jobTitle;à à } } In order to call the setJobTitle method, there must be an object of type Employee.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.