Q: The use of "This" keyword in Java is
-
A
It is used to declare a constant variable.
-
B
It refers to the current instance of the class.
-
C
It is used to called static method.
-
D
It represent the return value of a function.
B
Answer:
B
Explanation:
The this keyword in Java is used for a specific purpose related to the current instance of a class. Here's what each option means in the context of the this keyword:
a) It is used to declare a constant variable.
- This is incorrect. The
this keyword is not used to declare constant variables. Constant variables in Java are declared using the final keyword.
b) It refers to the current instance of the class.
- This is correct. The
this keyword is used to refer to the current instance of the class within its methods and constructors. It helps to distinguish between instance variables and parameters with the same name, and to call other constructors or methods within the same class.
c) It is used to call static methods.
- This is incorrect. Static methods can be called using the class name, not the
this keyword. The this keyword cannot be used to call static methods.
d) It represents the return value of a function.
- This is incorrect. The return value of a function is specified using the
return keyword, not this.
Correct Answer:
b) It refers to the current instance of the class.
Related Topic:
Share Above MCQ