Which keyword is used by the method to refer to the object that invoked it?
There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Show
Usage of Java this keywordHere is given the 6 usage of java this keyword.
Suggestion: If you are beginner to java, lookup only three usages of this keyword. 1) this: to refer current class instance variableThe this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Understanding the problem without this keywordLet's understand the problem if we don't use this keyword by the example given below: Test it NowOutput: In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable. Solution of the above problem by this keywordTest it NowOutput: 111 ankit 5000.0 112 sumit 6000.0 If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program: Program where this keyword is not requiredTest it NowOutput: 111 ankit 5000.0 112 sumit 6000.0 It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.2) this: to invoke current class methodYou may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example Test it NowOutput: 3) this() : to invoke current class constructorThe this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining. Calling default constructor from parameterized constructor: Output: Calling parameterized constructor from default constructor: Test it NowOutput: Real usage of this() constructor callThe this() constructor call should be used to reuse the constructor from the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see the example given below that displays the actual use of this keyword. Test it NowOutput: 111 ankit java 0.0 112 sumit java 6000.0 Rule: Call to this() must be the first statement in constructor.Test it NowOutput: Compile Time Error: Call to this must be first statement in constructor 4) this: to pass as an argument in the methodThe this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Let's see the example: Test it NowOutput: Application of this that can be passed as an argument:In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods. 5) this: to pass as argument in the constructor callWe can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example: Test it Now6) this keyword can be used to return current class instanceWe can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Let's see the example: Syntax of this that can be returned as a statementExample of this keyword that you return as a statement from the methodTest it NowOutput: Proving this keywordLet's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same. Test it NowOutput: Which keyword is used by method to point the object that invoked it?Which keyword is used by method to refer to the object that invokedit? a) importb) catchc) abstractd) thisAnswer: dExplanation: this keyword can be used inside any method to refer to thecurrent object. this is always a reference to the object on which themethod was invoked.
Which keyword is used by method to refer to the object that invoked it o Import catch abstract this?The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
Which keyword is used to invoke the object?Keyword 'THIS' in Java is a reference variable that refers to the current object. The various usage of keyword Java 'THIS' in Java is as per the below, It can be used to refer current class instance variable. It can be used to invoke or initiate current class constructor.
Which keyword is used to create an object of a class to invoke a method?Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
|