this Concept in java


SUBMITTED BY: coolvi4u

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

FORMAT: Java

SIZE: 8.8 kB

HITS: 9764

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

comments powered by Disqus