The Wayback Machine - https://web.archive.org/web/20240801202223/https://www.geeksforgeeks.org/difference-between-this-and-this-in-java/
Open In App

Difference Between this and this() in Java

Last Updated : 12 Mar, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In Java, both this and this() are completely different from each other.

  • this keyword is used to refer to the current object, i.e. through which the method is called.
  • this() is used to call one constructor from the other of the same class.

The below table shows the point-to-point difference between both this keyword and this().

this this()

this keyword is used with the objects only.

this() is used with constructors only.

It refers to the current object.

It refers to the constructor of the same class whose parameters matches with the parameters passed to this(parameters).

Dot(.) operator is used to access the members. 
For example, this.variableName;

No Dot(.) operator is used. Only the matching parameters are passed.

It is used to differentiate between the local variable and the instance variable in the method.

It is used to refer to the constructor belonging to the same class.

See the code below, which describes the utilization of this keyword.

Java




import java.io.*;
  
public class Student {
  
    private String name;
    private int age;
  
    // Note that in the Constructor below "this keyword" is
    // used to differentiate between the local variable and
    // the instance variable.
  
    public Student(String name, int age)
    {
        // Assigns the value of local name variable
        // to the name(instance variable).
        this.name = name;
        // Assigns the value of local Age variable
        // to the Age(instance variable).
        this.age = age;
    }
  
    public void show()
    {
        System.out.println("Name = " + this.name);
        System.out.println("Age = " + this.age);
    }
  
    public static void main(String[] args)
    {
        // Creating new instance of Student Class
        Student student = new Student("Geeks", 20);
        student.show();
    }
}


Output

Name = Geeks
Age = 20

Now take a look at the code below which describes the use of this().

Java




import java.io.*;
  
public class Student {
  
    private String name;
    private int age;
  
    // Constructor 1 with String as parameter.
    public Student(String name)
    {
        // This line of code calls the second constructor.
        this(20);
        System.out.println("Name of Student : " + name);
    }
  
    // Constructor 2 with int in parameter.
    public Student(int age)
    {
        System.out.println("Age of student = " + age);
    }
  
    // Constructor 3 with no parameters.
    public Student()
    {
        // This line calls the first constructor.
        this("Geeks");
    }
  
    public static void main(String[] args)
    {
        // This calls the third constructor.
        Student student = new Student();
    }
}


Output

Age of student = 20
Name of Student : Geeks

Please note that this() should always be the first executable statement in the constructor. Otherwise, the program will give compile time error. 



Previous Article
Next Article

Similar Reads

Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Difference between Core Java and Advanced Java
Core Java: Core Java is the part of Java programming language that is used for creating or developing a general-purpose application. Advanced Java: Advanced Java is also a part of Java programming language that generally deals with online application like the website and mobile application. Below is the difference between the two: Core JavaAdvanced
1 min read
Difference between a Java Application and a Java Applet
Java Application is just like a Java program that runs on an underlying operating system with the support of a virtual machine. It is also known as an application program. The graphical user interface is not necessary to execute the java applications, it can be run with or without it. Java Applet is a Java program that can be embedded into a web pa
3 min read
Difference between Java IO and Java NIO
Java IO(Input/Output) is used to perform read and write operations. The java.io package contains all the classes required for input and output operation. Whereas, Java NIO (New IO) was introduced from JDK 4 to implement high-speed IO operations. It is an alternative to the standard IO API's. In this article, the difference between these two IO pack
3 min read
Difference between Java and Core Java
Java is a very famous language that is based on the object-oriented programming concepts. It is the successor of the C and C++ languages. It was developed by Sun Microsystems. Core Java is a term used by Sun Microsystems to refer to the Java to standard edition J2SE. This is the parent of all other editions of Java used on internet sites. It has a
2 min read
Difference Between java.lang and java.util
Java Packages contain classes, interfaces, and sub-packages. The packages java.lang and java.util are two of the most commonly used packages in Java Standard Library. Both packages contain useful classes and utilities. In this article, we will learn about java.lang and java.util package with examples and also the difference between these two packag
2 min read
Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
In this article, we will learn, the difference between HashSet vs LinkedHashSet and TreeSet And similarities between LinkedHashSet and TreeSet. HashSet, LinkedHashSet, and TreeSet all implement the Set interface. So we have tried to list out the differences and similarities between HashSet, LinkedHashSet, and TreeSet in java. Differences Between Ha
6 min read
Similarities and Difference between Java and C++
Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as comepetitive programming . C++ is a widely popular language among coders for its efficiency, high speed, and dynamic memory utilization. Java is widely used in the
6 min read
Difference between Difference Engine and Analytical Engine
Introduction: The development of computing technology has a rich history, with many inventions and innovations leading to the creation of the modern computer. Two such machines, the Difference Engine and Analytical Engine, were created by the English mathematician and inventor Charles Babbage in the 19th century. While these machines share some sim
7 min read
Difference between Voltage Drop and Potential Difference
Voltage Drop is defined as the decrease in the electric potential along the path of current that is flowing in an electric circuit. Voltage drop can be assigned at each point in the electric circuit that is proportional to the electrical elevation. The amount of energy delivered per second to any component in the circuit is equal to the voltage dro
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg