This only exists inside the method and is used to represent the object that calls the method. It can be understood that there is a local variable called this inside each method. Whenever an object is initialized, the address of the object is passed to the this variable in each method of the object, so that this object can be used inside the method. .
#When to use this in java?
#1. When local variables and member variables have the same name, use this to represent the member variable in the method to distinguish them
Example:
class Demo{ String str = "这是成员变量"; void fun(String str){ System.out.println(str); System.out.println(this.str); this.str = str; System.out.println(this.str); } } public class This{ public static void main(String args[]){ Demo demo = new Demo(); demo.fun("这是局部变量"); } }
2. This keyword passes the current object to other methods
Example:
class Person{ public void eat(Apple apple){ Apple peeled = apple.getPeeled(); System.out.println("Yummy"); } } class Peeler{ static Apple peel(Apple apple){ //....remove peel return apple; } } class Apple{ Apple getPeeled(){ return Peeler.peel(this); } } public class This{ public static void main(String args[]){ new Person().eat(new Apple()); } }
3. When you need to return a reference to the current object, you often write return this
in the method. The advantage of this approach is that when you use an object to call this method, the method returns the modified object, and you can use the object to do other operations. Therefore it is easy to perform multiple operations on an object.
public class This{ int i = 0; This increment(){ i += 2; return this; } void print(){ System.out.println("i = " + i); } public static void main(String args[]){ This x = new This(); x.increment().increment().print(); } } 结果为:4
4. To call the constructor in the constructor, you need to use this
A class has many constructors. Sometimes you want to use a constructor in a constructor. To call other constructors in a function to avoid code duplication, you can use the this keyword.
Recommended tutorial: Java tutorial
The above is the detailed content of When to use this in java?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version