-2

I have read recently that this is a local variable which contain a reference ID of the current object and can be used inside in any instance function. But when I explicitly declare this as an int argument, I am getting compile time error stating: "The receiver type doesn't match the enclosing class type".

class ThisDemo
{
    void show(int this)
    {
        System.out.println(this);
    }
}
class ThisDemo1
{
    public static void main(String... s)
    {
        ThisDemo a=new ThisDemo();
        int x=10;
        a.show(x);
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 1
    Note that it is actually valid to declare a (receiver) parameter named `this`. It just has to be the correct type. https://ideone.com/WTGOV1 – Radiodef Jan 15 '19 at 17:09

3 Answers3

2

You might be confused by the error message

the receiver type doesn't match the enclosing class type

According to the other answers, you shouldn't be able to use this as a parameter (or declare it as a new variable), but the error message is saying something completely different.

In fact, you can use this as a parameter, but in only one place: as a receiver parameter.

The receiver parameter is an optional syntactic device for an instance method or an inner class's constructor. For an instance method, the receiver parameter represents the object for which the method is invoked. For an inner class's constructor, the receiver parameter represents the immediately enclosing instance of the newly constructed object. In both cases, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated (§9.7.4). The receiver parameter is not a formal parameter; more precisely, it is not a declaration of any kind of variable (§4.12.3), it is never bound to any value passed as an argument in a method invocation expression or class instance creation expression, and it has no effect whatsoever at run time.

Practically, it would look like

class ThisDemo
{
    void show(@Special ThisDemo this)
    {
        System.out.println(this);
    }
}

And you could then extract the @Special annotation through regular reflection means.

Savior
  • 3,225
  • 4
  • 24
  • 48
  • Why the hell would you ever do it? I think it's not good to encourage people using antipatterns. Simply do not ever name anything `this`. Also you still cannot do this in Java 7 and older versions. – aBnormaLz Jan 15 '19 at 18:21
  • @aBnormaLz: This isn't an antipattern...this appears to be a special case. It was *explicitly* added in Java 8, so saying that it didn't exist in Java 7 is moot, similar to how streams didn't exist in 7. – Makoto Jan 15 '19 at 19:56
  • 1
    @aBnormaLz Yeah, I don't understand why you call it an antipattern. This feature was specifically introduced to allow developers to annotate the receiver instance. This can have uses in Aspect Oriented Programming libraries for instance. Or some static analysis tools. – Savior Jan 15 '19 at 20:15
  • 1
    @aBnormaLz Also, I'm not debating whether you should _ever do it_, my answer simply explains the error message and what it's referring to. – Savior Jan 15 '19 at 20:20
-1

You cannot use this to name your variable, since it's a reserved keyword in Java. this refers to your current object (in your case the object of class ThisDemo). I guess what you want to achieve is the following:

class ThisDemo
{
    void show()
    {
        System.out.println(this);
    }
}
class ThisDemo1
{
    public static void main(String... s)
    {
        ThisDemo a=new ThisDemo();
        a.show();
    }
}
FilipR
  • 1,218
  • 4
  • 22
  • 39
  • 1
    There *is* an explicit case in which you can use `this` as the name of a variable. Reference Savior's answer which also talks about receiver parameters (which is the only other use case of `this`). – Makoto Jan 15 '19 at 17:30
-1

this is a keyword that refers to the current instance of a method or object. It's used to refer to the object that it is a part of.

So think of a human body as a class. Because instances of the object may be called different things like John or Kyle when referring to a generic human in a method you would use this. Example, to get your heartbeat for any human you would have something like this.getHeartbeat().

Hopefully this helps you conceptualize this.