Data Shadowing

  1. class Ds
  2. {
  3. int x=50;
  4. void show(int y)
  5. {
  6. int x=70;
  7. System.out.println(x);
  8. System.out.println(y);
  9. }
  10. public static void main(String...a)
  11. {
  12. Ds d1 = new Ds();
  13. d1.show(30);
  14. }
  15. }
  16. Output:
  17. 70
  18. 30
  1. class Ds
  2. {
  3. int x=50;
  4. void show(int y)
  5. {
  6. int x=70;
  7. System.out.println(this.x);
  8. System.out.println(x);
  9. System.out.println(y);
  10. }
  11. public static void main(String...a)
  12. {
  13. Ds d1 = new Ds();
  14. d1.show(30);
  15. }
  16. }
  17. Output:
  18. 50
  19. 70
  20. 30
  1. class Temp
  2. {
  3. int a=10;
  4. void method1()
  5. {
  6. int a=20;
  7. System.out.println(a);
  8. System.out.println(this.a);
  9. }
  10. public static void main(String...a)
  11. {
  12. Temp t1 = new Temp();
  13. Temp t2 = new Temp();
  14. t1.method1();
  15. t2.method1();
  16. }
  17. }
  18. Output:
  19. 20
  20. 10
  21. 20
  22. 10
  1. class Temp
  2. {
  3. int a=10;
  4. void method1()
  5. {
  6. int x=20;
  7. System.out.println(a);
  8. System.out.println(this.a);
  9. }
  10. public static void main(String...a)
  11. {
  12. Temp t1 = new Temp();
  13. Temp t2 = new Temp();
  14. t1.method1();
  15. t2.method1();
  16. }
  17. }
  18. Output:
  19. 10
  20. 10
  21. 10
  22. 10
  1. class Temp
  2. {
  3. int x=10;
  4. void method1()
  5. {
  6. int a=20;
  7. System.out.println(a);
  8. System.out.println(this.a);
  9. }
  10. public static void main(String...a)
  11. {
  12. Temp t1 = new Temp();
  13. Temp t2 = new Temp();
  14. t1.method1();
  15. t2.method1();
  16. }
  17. }
  18. Output:
  19. Temp.java:8: error: cannot find symbol System.out.println(this.a); ^ symbol: variable a 1 error
  1. class Demo
  2. {
  3. int a, b;
  4. void get (int x, int y)
  5. {
  6. a=x;
  7. b=y;
  8. }
  9. void print()
  10. {
  11. System.out.println(a);
  12. System.out.println(b);
  13. }
  14. public static void main(String...a)
  15. {
  16. Demo d1 = new Demo();
  17. Demo d2 = new Demo();
  18. d1.get(10,20);
  19. d2.get(30,40);
  20. d1.print();
  21. d2.print();
  22. }
  23. }
  24. Output:
  25. 10
  26. 20
  27. 30
  28. 40
  1. class Demo
  2. {
  3. int a, b;
  4. void get (int x, int y, Demo this)
  5. {
  6. this.a=x;
  7. this.b=y;
  8. }
  9. void print(Demo this)
  10. {
  11. System.out.println(this.a);
  12. System.out.println(this.b);
  13. }
  14. public static void main(String...a)
  15. {
  16. Demo d1 = new Demo();
  17. Demo d2 = new Demo();
  18. d1.get(10,20,d1);
  19. d2.get(30,40,d2);
  20. d1.print(d1);
  21. d2.print(d2);
  22. }
  23. }
  24. Output:
  25. Demo.java:4: error: void get (int x, int y, Demo this) ^ 1 error
  26. //as of release 8, 'this' is allowed as the parameter name for the receiver type only, which has to be the first parameter
  1. class Demo
  2. {
  3. int a, b;
  4. void get (int x, int y)
  5. {
  6. this.a=x;
  7. this.b=y;
  8. }
  9. void print(Demo this)
  10. {
  11. System.out.println(this.a);
  12. System.out.println(this.b);
  13. }
  14. public static void main(String...a)
  15. {
  16. Demo d1 = new Demo();
  17. Demo d2 = new Demo();
  18. d1.get(10,20);
  19. d2.get(30,40);
  20. d1.print();
  21. d2.print();
  22. }
  23. }
  24. Output:
  25. 10
  26. 20
  27. 30
  28. 40
  1. class Xyz
  2. {
  3. int a=50;
  4. void show(Xyz x)
  5. {
  6. int a=70;
  7. System.out.println(x.a);
  8. }
  9. public static void main(String...a)
  10. {
  11. Xyz x1 = new Xyz();
  12. x1.show(x1);
  13. }
  14. }
  15. Output:
  16. 50
  1. class Xyz
  2. {
  3. static int a=50;
  4. static void show(Xyz x)
  5. {
  6. int a=70;
  7. System.out.println(a);
  8. }
  9. public static void main(String...a)
  10. {
  11. Xyz x1 = new Xyz();
  12. show(x1);
  13. }
  14. }
  15. Output
  16. 70
  1. class Xyz
  2. {
  3. int a=10;
  4. static int b=50;
  5. void show ()
  6. {
  7. int a=20;
  8. System.out.println(this.a);
  9. System.out.println(this.b);
  10. }
  11. public static void main(String...a)
  12. {
  13. Xyz x1 = new Xyz();
  14. show();
  15. }
  16. }
  17. Output
  18. Xyz.java:14: error: non-static method show() cannot be referenced from a static context show(); ^ 1 error

| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |

Free Web Hosting