"KEEP LEARNING, DON'T STOP."
"KEEP LEARNING, DON'T STOP."
" this " is a keyword not a modifier. " this " is one self-reference variable. Every object will have self-reference variable called " this " keyword. this mean current object. We use " this " keyword for current class. You can't use " this " keyword to call the another class member.
Let's suppose you have debit card with you. You only able to use it, because it's your debit card. Another person can't use it because it is belong to you only.
In same way " this " keyword also belong to current class not to another class.
We can use " this " keyword in program in 4 ways.
If you will not use " this " keyword then JVM will assign default value to the instance variable. Our instance variable and method local variable having same name, to differentiate them we use this keyword. It show that it's belong to current class instance variable.
class Book { float price; String name; public void getDetail(int price, String name) { this.price = price; this.name = name; } public String toString() { return "Book Price = "+price+"\nBook Name = "+name; } } class BookDetail { public static void main(String arg[]) { Book bk = new Book(); bk.getDetail(255,"Java Book"); System.out.println(bk); } }
Book Price = 255.0 Book Name = Java Book
Book Price = 0.0 Book Name = null
" this " keyword is use to call the current class method in another method within the same class.
class One { public void methodOne() { System.out.println("Method One Calling...."); } public void methodTwo() { System.out.println("Method Two Calling...."); this.methodOne(); } } class MethodDetail { public static void main(String arg[]) { One o = new One(); o.methodTwo(); } }
Method Two Calling.... Method One Calling....
" this " keyword is use to call the current class constructor in another constructor within the same class.
class Student { public Student() { System.out.println("0 Parametrized Constructor..."); } public Student(int a) { this(); //calling 0 perametrized constructor System.out.println("Parametrized Constructor..."); } } class CurrentClassConstructor { public static void main(String arg[]) { Student st = new Student(10); } }
0 Parametrized Constructor... Parametrized Constructor...
When you execute or run this program it will give error. See error below the program. See the point number 4.
class Student { public Student() { System.out.println("0 Parametrized Constructor..."); } public Student(int i) { System.out.println("Parametrized Constructor..."); this(); } }
C:\Users\Nature\Desktop\Java>javac CurrentClassConstructor.java CurrentClassConstructor.java:12: error: call to this must be first statement in constructor this(); //calling 0 perametrized constructor ^ 1 error
It can be used to refer to its own object or current class object.
class ClassOne { int i = 10; String name = "Hello"; ClassTwo c2; //calling another class in this class. public void m1() { System.out.println("M1 method..."); c2 = new ClassTwo(); c2.m2(this); } } class ClassTwo { public void m2(ClassOne o1) { System.out.println("M2 method..."); } } class CurrentClassObject { public static void main(String arg[]) { ClassOne c1 = new ClassOne(); c1.m1(); } }
M1 method... M2 method...