[java] When should I use "this" in a class?

“This” keyword in java is used to refer current class objects.

There are 6 uses of “this” keyword in java

  1. Accessing class level variable: mostly used if local and class level variable is same
  2. Accessing class methods : this is default behavior and can be ignored
  3. For calling other constructor of same class
  4. Using ‘this’ keyword as return value : for returning current instance from method
  5. Passing ‘this’ keyword as argument to method Passing : for passing current class instance as argument
  6. this keyword as argument to constructor : for passing current class instance as argument

ref: https://stacktraceguru.com/java/this-keyword-in-java

Examples related to java

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

Examples related to oop

How to implement a simple scenario the OO way When to use 'raise NotImplementedError'? PHP: cannot declare class because the name is already in use Python class input argument Call an overridden method from super class in typescript Typescript: How to extend two classes? What's the difference between abstraction and encapsulation? An object reference is required to access a non-static member Java Multiple Inheritance Why not inherit from List<T>?

Examples related to this

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?