[java] this in equals method

I am looking at the equals method, and I see this and I don't understand what it means...I do understand them when I see it in constructors and some methods but its not clear to me when they are in equals method something like this:

(obj == this) ...what does this mean here ? where does it come from ?

I understand when it says something like this.name = name;

from a method like this

@Override public boolean equals(Object obj) {     if (obj == this) {         return true;     }     if (obj == null || obj.getClass() != this.getClass()) {         return false;     } 

sorry it might be a duplicate but I couldnt find anything...

This question is related to java this equals

The answer is

this is the current Object instance. Whenever you have a non-static method, it can only be called on an instance of your object.

You are comparing two objects for equality. The snippet:

if (obj == this) {     return true; } 

is a quick test that can be read

"If the object I'm comparing myself to is me, return true"

. You usually see this happen in equals methods so they can exit early and avoid other costly comparisons.

this refers to the current instance of the class (object) your equals-method belongs to. When you test this against an object, the testing method (which is equals(Object obj) in your case) will check wether or not the object is equal to the current instance (referred to as this).

An example:

Object obj = this; this.equals(obj); //true   Object obj = this; new Object().equals(obj); //false 

Questions with java tag:

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error Instantiating a generic type When to create variables (memory management) java doesn't run if structure inside of onclick listener String method cannot be found in a main class method Are all Spring Framework Java Configuration injection examples buggy? Calling another method java GUI I need to know how to get my program to output the word i typed in and also the new rearranged word using a 2D array Java and unlimited decimal places? Read input from a JOptionPane.showInputDialog box Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable Two Page Login with Spring Security 3.2.x Hadoop MapReduce: Strange Result when Storing Previous Value in Memory in a Reduce Class (Java) Got a NumberFormatException while trying to parse a text file for objects Best way for storing Java application name and version properties Call japplet from jframe FragmentActivity to Fragment Comparing two joda DateTime instances Maven dependencies are failing with a 501 error IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Why am I getting Unknown error in line 1 of pom.xml? Gradle: Could not determine java version from '11.0.2' Error: Java: invalid target release: 11 - IntelliJ IDEA Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Why is 2 * (i * i) faster than 2 * i * i in Java? must declare a named package eclipse because this compilation unit is associated to the named module How do I install Java on Mac OSX allowing version switching? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Difference between OpenJDK and Adoptium/AdoptOpenJDK OpenJDK8 for windows How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Find the smallest positive integer that does not occur in a given sequence Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 How to uninstall Eclipse? Failed to resolve: com.google.firebase:firebase-core:16.0.1 How to resolve Unable to load authentication plugin 'caching_sha2_password' issue

Questions with this tag:

this in equals method React: "this" is undefined inside a component function How to access the correct `this` inside a callback? jQuery $(this) keyword Difference between $(this) and event.target? 'this' vs $scope in AngularJS controllers Difference between getContext() , getApplicationContext() , getBaseContext() and "this" Use of "this" keyword in C++ What is context in _.each(list, iterator, [context])? What does 'var that = this;' mean in JavaScript? What is the meaning of "this" in Java? How do I pass the this context to a function? How does the "this" keyword work? jQuery remove selected option from this When should I use "this" in a class? Pass correct "this" context to setTimeout callback? What does the variable $this mean in PHP? What's the difference between '$(this)' and 'this'? Use of "this" keyword in formal parameters for static methods in C# How can I exclude $(this) from a jQuery selector? Use of 'prototype' vs. 'this' in JavaScript? When do you use the "this" keyword?

Questions with equals tag:

this in equals method Why do we have to override the equals() method in Java? Compare two objects with .equals() and == operator Check if bash variable equals 0 Setting equal heights for div's with jQuery Java, how to compare Strings with String Arrays How can I express that two values are not equal to eachother? How to override equals method in Java How do you say not equal to in Ruby? Getting an element from a Set BigDecimal equals() versus compareTo() Comparing two strings, ignoring case in C# How do I test if a variable does not equal either of two values? Create the perfect JPA entity Hashcode and Equals for Hashset Difference between null and empty ("") Java String Compare two List<T> objects for equality, ignoring order bash string equality How to compare two maps by their values How to check if my string is equal to null? Why do I need to override the equals and hashCode methods in Java? Why would you use String.Equals over ==? What's the difference between ".equals" and "=="? compareTo() vs. equals() Integer value comparison C# difference between == and Equals() Any reason to prefer getClass() over instanceof when generating .equals()? Equals(=) vs. LIKE How to determine equality for two JavaScript objects? Overriding the java equals() method - not working? Best implementation for hashCode method for a collection What issues should be considered when overriding equals and hashCode in Java?