What is the meaning of “this” in Java?

后端 未结 21 2564
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

相关标签:
21条回答
  • 2020-11-21 06:03

    If the instance variables are same as the variables that are declared in the constructor then we use "this" to assign data.

    class Example{
         int assign;// instance variable
    
         Example(int assign){ // variable inside constructor
              this.assign=assign;
         }
    }
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-21 06:04

    Quoting an article at programming.guide:


    this has two uses in a Java program.

    1. As a reference to the current object

    The syntax in this case usually looks something like

    this.someVariable = someVariable;
    

    This type of use is described here: The 'this' reference (with examples)

    2. To call a different constructor

    The syntax in this case typically looks something like

    MyClass() {
        this(DEFAULT_VALUE); // delegate to other constructor
    }
    
    MyClass(int value) {
        // ...
    }
    

    This type of use is described here: this(…) constructor call (with examples)

    0 讨论(0)
  • 2020-11-21 06:06

    This refers to the object you’re “in” right now. In other words,this refers to the receiving object. You use this to clarify which variable you’re referring to.Java_whitepaper page :37

    class Point extends Object
    {
        public double x;
        public double y;
    
        Point()
        {
            x = 0.0;
            y = 0.0;
        }
    
        Point(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
    

    In the above example code this.x/this.y refers to current class that is Point class x and y variables where (double x,double y) are double values passed from different class to assign values to current class .

    0 讨论(0)
  • 2020-11-21 06:08

    A quick google search brought this result: http://xahlee.org/java-a-day/this.html

    Pretty much the "this" keyword is a reference to the current object (itself).

    0 讨论(0)
  • 2020-11-21 06:10

    In Swing its fairly common to write a class that implements ActionListener and add the current instance (ie 'this') as an ActionListener for components.

    public class MyDialog extends JDialog implements ActionListener
    {
        public MyDialog()
        {
            JButton myButton = new JButton("Hello");
            myButton.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent evt)
        {
            System.out.println("Hurdy Gurdy!");
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 06:13

    Instance variables are common to every object that you creating. say, there is two instance variables

    class ExpThisKeyWord{
    int x;
    int y;
    
    public void setMyInstanceValues(int a, int b) {
        x= a;
        y=b;
    
        System.out.println("x is ="+x);
        System.out.println("y is ="+y);
    }
    
    }
    
    
    
    
    class Demo{
    public static void main(String[] args){
    
    ExpThisKeyWord obj1 = new ExpThisKeyWord();
    ExpThisKeyWord obj2 = new ExpThisKeyWord();
    ExpThisKeyWord obj3 = new ExpThisKeyWord();
    
    obj1.setMyInstanceValues(1, 2);
    obj2.setMyInstanceValues(11, 22);
    obj3.setMyInstanceValues(111, 222);
    
    
    
    }
    }
    

    if you noticed above code, we have initiated three objects and three objects are calling SetMyInstanceValues method. How do you think JVM correctly assign the values for every object? there is the trick, JVM will not see this code how it is showed above. instead of that, it will see like below code;

    public void setMyInstanceValues(int a, int b) {
        this.x= a; //Answer: this keyword denotes the current object that is handled by JVM.
        this.y=b;
    
        System.out.println("x is ="+x);
        System.out.println("y is ="+y);
    }
    
    0 讨论(0)
提交回复
热议问题