Java this

  • 71 views
Uploaded on

Why we use this in java …

Why we use this in java

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
71
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. What is this this is a keyword in Java. Which can be used inside method or constructor of class. It(this) works as a reference to current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor. this keyword with field(Instance Variable) this keyword can be very useful in case of Variable Hiding. We can not create two Instance/Local Variable with same name. But it is legal to create One Instance Variable & One Local Variable or Method parameter with same name. In this scenario Local Variable will hide the Instance Variable which is called Variable Hiding. Example of Variable Hiding class JBT { int variable = 5; public static void main(String args[]) { JBT obj = new JBT(); obj.method(20); obj.method(); } void method(int variable) { variable = 10; System.out.println("Value of variable:" + variable); } void method() {
  • 2. int variable = 40; System.out.println("Value of variable:" + variable); } } Output of the above programme Value of variable:10 Value of variable:40 As you can Instance Variable is hiding here and value getting displayed is the value of Local Variable(or Method Parameter) and not Instance Variable. To solve this problem this keyword can be used with field to point Instance Variable instead of Local Variable. Example of this keyword for Variable Hiding class JBT { int variable = 5; public static void main(String args[]) { JBT obj = new JBT(); obj.method(20); obj.method(); } void method(int variable) { variable = 10; System.out.println("Value of Instance variable:" + this.variable); System.out.println("Value of Local variable:" + variable); }
  • 3. void method() { int variable = 40; System.out.println("Value of Instance variable:" + this.variable); System.out.println("Value of Local variable:" + variable); } } Output of the above programme Value of Instance variable:5 Value of Local variable:10 Value of Instance variable:5 Value of Local variable:40 this keyword with Constructor this keyword can be used inside Constructor to call another overloaded Constructor in same class. It is called Explicit Constructor Invocation. If a class has two overloaded constructor one without argument and another with argument. Then this keyword can be used to call Constructor with argument from Constructor without argument. As constructor can not be called explicitly. Example of this with Constructor class JBT { JBT() { this("JBT"); System.out.println("Inside Constructor without parameter"); } JBT(String str) { System.out
  • 4. .println("Inside Constructor with String parameter as " + str); } public static void main(String[] args) { JBT obj = new JBT(); } } Output of above programme Inside Constructor with parameter JBT Inside Constructor without parameter As you can see this can be used to invoke overloaded constructor in the same class. Note*: this keyword can only be the first statement in Constructor. A constructor can have either this or super keyword but not both. this keyword with Method this keyword can also be used inside methods to call another method from same class. Example of this keyword with Method class JBT { public static void main(String[] args) { JBT obj = new JBT(); obj.methodTwo(); } void methodOne(){ System.out.println("Inside Method ONE");
  • 5. } void methodTwo(){ System.out.println("Inside Method TWO"); this.methodOne();// same as calling methodOne() } } Output of above programs Inside Method TWO Inside Method ONE