✔ JAVA CHAPTERS:-
1. Introduction to Java
2. Features of Java
3. Java Virtual Machine
4. D/F B/W JVM, JRE, JDK
5. Java Data Types
6. Java Programs
7. Setting Java Path
8. OOPs Concepts
9. Object & Class In Java
10. OOPs Continued
11. Encapsulation & Polymorphism
12. Method Overloading
13. Method Overriding
14. Dynamic Method Dispatch
15. Association In Java
16. Inheritance in Java
17. Aggregation in java
18. Command Line Argument
19. Execute Command Line
20. Read Input From Command Line
21. Abstract Class In Java
22. Interface in Java
23. Cutom Marker Interface
24. Constructor In Java
25. Package in Java
26. Acess Modifier In Java
27. Static Import In Java
28. Package Class In Java
29. This Keyword
30. Instance Intializer Block
31. Super Keyword
32. Static Keyword
33. Final Keyword
1. Introduction to Java
2. Features of Java
3. Java Virtual Machine
4. D/F B/W JVM, JRE, JDK
5. Java Data Types
6. Java Programs
7. Setting Java Path
8. OOPs Concepts
9. Object & Class In Java
10. OOPs Continued
11. Encapsulation & Polymorphism
12. Method Overloading
13. Method Overriding
14. Dynamic Method Dispatch
15. Association In Java
16. Inheritance in Java
17. Aggregation in java
18. Command Line Argument
19. Execute Command Line
20. Read Input From Command Line
21. Abstract Class In Java
22. Interface in Java
23. Cutom Marker Interface
24. Constructor In Java
25. Package in Java
26. Acess Modifier In Java
27. Static Import In Java
28. Package Class In Java
29. This Keyword
30. Instance Intializer Block
31. Super Keyword
32. Static Keyword
33. Final Keyword
Package Class in Java: Previous Next: Instance Initializer Block
This Keyword In Java
this keyword:
this is a keyword in java which refers to the current instance of the class. Every constructor and all non-static methods in java have this as an implicitly parameter.
Note: When n numbers of parameters are passed in a method then from JRE point of view n+1 number of parameters are passed in the method. One additional parameter is this. If this keyword is used in constructor or method then this must be the first statement.
Use of this keyword in java:
- 1. this can be used to differentiate between instance variable and local variable.
- 2. this can be used in constructor chaining. this() and super() must be the first statement.
- 3. this can be used to invoke current class method implicitly.
- 4. this can be used to return current instance of the class.
- 5. this can be used as a parameter in constructor call.
- 6. this can be used as a parameter in method call.
1. this can be used to differentiate between instance variable and local variable.
If local variable and instance variables are same than compiler will not be able to distinguish them.
Example:
ThisExample1.java
Output:
If local variable and instance variables are same than compiler will not be able to distinguish them. this can be used to differentiate between instance variable and local variable
Example:
ThisExample2.java
Output:
If local variable and instance variables are different than no need of this keyword.
Example:
ThisExample3.java
Output:
2. this can be used in constructor chaining.
Constructor chaining:
Constructor chaining is a process of calling one constructor from other constructor of a class on an object.
Example:
ThisExample7.java
Output:
3. this can be used to invoke current class method implicitly.
Method chaining:
Method chaining is a process of calling multiple methods on an object in a single statement.
Example:
ThisExample4.java
Output:
4. this can be used to return current instance of the class.
Example:
ThisExample6.java
Output:
5. this can be used as a parameter in constructor call.
Example:
ThisExample8.java
Output:
6. this can be used as a parameter in method call.
Example:
ThisExample5.java