This Keyword Example Java Program
Definition
The keyword this is a Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods. Since all instance methods are virtual in Java, this can never be null.Syntax
this.Variable_name = Variable_name;
This Keyword Example Program
class ClassOne { public void getClassName() { System.out.println("Class : " + this.getClass()); } } class ClassTwo { public void getClassName() { System.out.println("Class : " + this.getClass()); } } public class ThisKeywordDemo { public static void main(String args[]) { ClassOne obj1 = new ClassOne(); ClassTwo obj2 = new ClassTwo(); obj1.getClassName(); obj2.getClassName(); } }
Sample Output
Output is: Class : classOne Class : classTwo
Miscellaneous Programs
- Super Keyword Example Java Program
- This Keyword Example Java Program
- Sleep Function Example Java Program
- IP Address Example Java Program
- Print Date Example Java Program
- Print Time Example Java Program
- Print Week Example Java Program
- User Defined Package Example Java Program
- Command Line Arguments Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Twin Prime Example Java Program
- Heap Sort Example Java Program
- Compile Time Polymorphism Example Java Program