this keyword in java


SUBMITTED BY: coolvi4u

DATE: Nov. 27, 2015, 7:12 a.m.

FORMAT: Text only

SIZE: 8.8 kB

HITS: 9670

  1. this keyword in java
  2. this keyword
  3. Usage of this keyword
  4. to refer the current class instance variable
  5. to invoke the current class constructor
  6. to invoke the current class method
  7. to pass as an argument in the method call
  8. to pass as an argument in the constructor call
  9. to return the current class instance
  10. Proving this keyword
  11. There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object.
  12. Usage of java this keyword
  13. Here is given the 6 usage of java this keyword.
  14. this keyword can be used to refer current class instance variable.
  15. this() can be used to invoke current class constructor.
  16. this keyword can be used to invoke current class method (implicitly)
  17. this can be passed as an argument in the method call.
  18. this can be passed as argument in the constructor call.
  19. this keyword can also be used to return the current class instance.
  20. Suggestion: If you are beginner to java, lookup only two usage of this keyword.
  21. java this keyword
  22. 1) The this keyword can be used to refer current class instance variable.
  23. If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.
  24. Understanding the problem without this keyword
  25. Let's understand the problem if we don't use this keyword by the example given below:
  26. class Student10{
  27. int id;
  28. String name;
  29. Student10(int id,String name){
  30. id = id;
  31. name = name;
  32. }
  33. void display(){System.out.println(id+" "+name);}
  34. public static void main(String args[]){
  35. Student10 s1 = new Student10(111,"Karan");
  36. Student10 s2 = new Student10(321,"Aryan");
  37. s1.display();
  38. s2.display();
  39. }
  40. }
  41. Test it Now
  42. Output:0 null
  43. 0 null
  44. In the above example, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable.
  45. Solution of the above problem by this keyword
  46. //example of this keyword
  47. class Student11{
  48. int id;
  49. String name;
  50. Student11(int id,String name){
  51. this.id = id;
  52. this.name = name;
  53. }
  54. void display(){System.out.println(id+" "+name);}
  55. public static void main(String args[]){
  56. Student11 s1 = new Student11(111,"Karan");
  57. Student11 s2 = new Student11(222,"Aryan");
  58. s1.display();
  59. s2.display();
  60. }
  61. }
  62. Test it Now
  63. Output111 Karan
  64. 222 Aryan
  65. this keyword
  66. If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:
  67. Program where this keyword is not required
  68. class Student12{
  69. int id;
  70. String name;
  71. Student12(int i,String n){
  72. id = i;
  73. name = n;
  74. }
  75. void display(){System.out.println(id+" "+name);}
  76. public static void main(String args[]){
  77. Student12 e1 = new Student12(111,"karan");
  78. Student12 e2 = new Student12(222,"Aryan");
  79. e1.display();
  80. e2.display();
  81. }
  82. }
  83. Test it Now
  84. Output:111 Karan
  85. 222 Aryan
  86. 2) this() can be used to invoked current class constructor.
  87. The this() constructor call can be used to invoke the current class constructor (constructor chaining). This approach is better if you have many constructors in the class and want to reuse that constructor.
  88. //Program of this() constructor call (constructor chaining)
  89. class Student13{
  90. int id;
  91. String name;
  92. Student13(){System.out.println("default constructor is invoked");}
  93. Student13(int id,String name){
  94. this ();//it is used to invoked current class constructor.
  95. this.id = id;
  96. this.name = name;
  97. }
  98. void display(){System.out.println(id+" "+name);}
  99. public static void main(String args[]){
  100. Student13 e1 = new Student13(111,"karan");
  101. Student13 e2 = new Student13(222,"Aryan");
  102. e1.display();
  103. e2.display();
  104. }
  105. }
  106. Test it Now
  107. Output:
  108. default constructor is invoked
  109. default constructor is invoked
  110. 111 Karan
  111. 222 Aryan
  112. Where to use this() constructor call?
  113. The this() constructor call should be used to reuse the constructor in 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.
  114. class Student14{
  115. int id;
  116. String name;
  117. String city;
  118. Student14(int id,String name){
  119. this.id = id;
  120. this.name = name;
  121. }
  122. Student14(int id,String name,String city){
  123. this(id,name);//now no need to initialize id and name
  124. this.city=city;
  125. }
  126. void display(){System.out.println(id+" "+name+" "+city);}
  127. public static void main(String args[]){
  128. Student14 e1 = new Student14(111,"karan");
  129. Student14 e2 = new Student14(222,"Aryan","delhi");
  130. e1.display();
  131. e2.display();
  132. }
  133. }
  134. Test it Now
  135. Output:111 Karan null
  136. 222 Aryan delhi
  137. Rule: Call to this() must be the first statement in constructor.
  138. class Student15{
  139. int id;
  140. String name;
  141. Student15(){System.out.println("default constructor is invoked");}
  142. Student15(int id,String name){
  143. id = id;
  144. name = name;
  145. this ();//must be the first statement
  146. }
  147. void display(){System.out.println(id+" "+name);}
  148. public static void main(String args[]){
  149. Student15 e1 = new Student15(111,"karan");
  150. Student15 e2 = new Student15(222,"Aryan");
  151. e1.display();
  152. e2.display();
  153. }
  154. }
  155. Test it Now
  156. Output:Compile Time Error
  157. 3)The this keyword can be used to invoke current class method (implicitly).
  158. You 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
  159. this keyword
  160. class S{
  161. void m(){
  162. System.out.println("method is invoked");
  163. }
  164. void n(){
  165. this.m();//no need because compiler does it for you.
  166. }
  167. void p(){
  168. n();//complier will add this to invoke n() method as this.n()
  169. }
  170. public static void main(String args[]){
  171. S s1 = new S();
  172. s1.p();
  173. }
  174. }
  175. Test it Now
  176. Output:method is invoked
  177. 4) this keyword can be passed as an argument in the method.
  178. The 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:
  179. class S2{
  180. void m(S2 obj){
  181. System.out.println("method is invoked");
  182. }
  183. void p(){
  184. m(this);
  185. }
  186. public static void main(String args[]){
  187. S2 s1 = new S2();
  188. s1.p();
  189. }
  190. }
  191. Test it Now
  192. Output:method is invoked
  193. Application of this that can be passed as an argument:
  194. In event handling (or) in a situation where we have to provide reference of a class to another one.
  195. 5) The this keyword can be passed as argument in the constructor call.
  196. We 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:
  197. class B{
  198. A4 obj;
  199. B(A4 obj){
  200. this.obj=obj;
  201. }
  202. void display(){
  203. System.out.println(obj.data);//using data member of A4 class
  204. }
  205. }
  206. class A4{
  207. int data=10;
  208. A4(){
  209. B b=new B(this);
  210. b.display();
  211. }
  212. public static void main(String args[]){
  213. A4 a=new A4();
  214. }
  215. }
  216. Test it Now
  217. Output:10
  218. 6) The this keyword can be used to return current class instance.
  219. We can return the 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:
  220. Syntax of this that can be returned as a statement
  221. return_type method_name(){
  222. return this;
  223. }
  224. Example of this keyword that you return as a statement from the method
  225. class A{
  226. A getA(){
  227. return this;
  228. }
  229. void msg(){System.out.println("Hello java");}
  230. }
  231. class Test1{
  232. public static void main(String args[]){
  233. new A().getA().msg();
  234. }
  235. }
  236. Test it Now
  237. Output:Hello java
  238. Proving this keyword
  239. Let'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.
  240. class A5{
  241. void m(){
  242. System.out.println(this);//prints same reference ID
  243. }
  244. public static void main(String args[]){
  245. A5 obj=new A5();
  246. System.out.println(obj);//prints the reference ID
  247. obj.m();
  248. }
  249. }
  250. Test it Now
  251. Output:A5@22b3ea59
  252. A5@22b3ea59

comments powered by Disqus