Java This Keyword

In this tutorial, you will learn about this keyword in java programming language.

You will learn what is this keyword, why it is used and how to use it, how to access the class members using this in detail with example.

Source Code for Source Code for Student.java

 1 package oops;
 2 
 3 public class Student {
 4 
 5     String name;
 6     int age;  
 7     
 8     Student(){
 9         
10     }
11     
12     Student(String name){
13         this();
14         this.name = name;
15     }
16     
17     Student(int age){
18         this("noname");
19         this.age = age;
20     }
21     
22     Student(String name, int age){
23         this(age);
24         System.out.println("Constructor with 2 parameters");
25     }  
26     
27     void introduce(){   
28         System.out.println("name is "+this.name+" and age is "+this.age);
29     }
30     
31     static void howManyStudents(){
32    //this
33     }
34 }


Source Code for Source Code for Tutorials.java

1 package oops;
2 
3 public class Tutorials {
4 
5     public static void main(String[] args) {  
6         Student anil = new Student("anil",24);  
7         anil.introduce();
8     }
9 }

Watch this video to learn how this works